Link to home
Start Free TrialLog in
Avatar of Juzer Ramp
Juzer Ramp

asked on

highlight duplicate entry

I have VBA code to highlight duplicate entry , but this highlight also the number which is in hidden criteria , can you advice a new code,

below is the code required update

Private Sub Worksheet_Change(ByVal Target As Range)
   
    If Target.Row = 1 Then Exit Sub ' IF ITS A HEADER, DO NOTHING.
   
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
   
    Dim myDataRng As Range
    Dim cell As Range
     
    ' WE WILL SET THE RANGE (SECOND COLUMN).
    Set myDataRng = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
     
    For Each cell In myDataRng
        cell.Offset(0, 0).Font.Color = vbBlack ' DEFAULT COLOR.
   
        ' LOCATE DUPLICATE VALUE(S) IN THE SPECIFIED RANGE OF DATA.
        If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then
            cell.Offset(0, 0).Font.Color = vbRed ' CHANGE FORE COLOR TO RED.
        End If
    Next cell
     
    Set myDataRng = Nothing
ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Will you consider to use Conditional Formatting since you can do COUNTIF within it?

but this highlight also the number which is in hidden criteria
Can you clarify what do you mean by "number" here?
Avatar of Juzer Ramp
Juzer Ramp

ASKER

Hello Sir
Attached is the file , where you can see VBA code is running to highlight duplicate entry in column B , now B11 is highlighted red which is correct because number is repeated same in B3
Now also B12 is highlighted red , however the number is repeated but it is in hidden cell B4, so i want to exclude it. I mean hidden cell should not be picked as duplicate and should not be highlighted.
Highlight-Duplicate-VBA.xls
I do that using Conditional Formatting.

with formula such as:

=SUMPRODUCT((B$3:B$1000=B3)*(SUBTOTAL(103,OFFSET(B$3,ROW(B$3:B$1000)-MIN(ROW(B$3:B$1000)),0))))>1

I will try to implement above using VBA if got time.

hope this helps
Highlight-Duplicate-VBA_b.xls
Thank You the conditional formatting formula you provided is not working.
it's working fine.

User generated image
User generated image
User generated image
make sure you apply the range correctly
User generated image
Still not working My Excel is Version 2000 may be that's why its not working ?
User generated image
I don't have an Excel ver 2000 with me... but can you have to do some test for me?

Try make another Conditional Formatting with following formula:

=A2>TODAY()

Open in new window


and in A2, try to enter a future date, and see if that date will be highlighted?

another way for debugging is try to copy the formula out and paste it into normal cells:

=SUMPRODUCT((B$3:B$1000=B3)*(SUBTOTAL(103,OFFSET(B$3,ROW(B$3:B$1000)-MIN(ROW(B$3:B$1000)),0))))

Open in new window


see if you get the expected values? (number of visible cells with same value)
No both formula your provided is not working
However I found one another formula for highlighting duplicate values in conditional formatting  i.e =COUNTIF($B$1:$B$98,B13)>1
But only issue is it also highlight duplicates values which is in hidden cell
see attachment , in B5 hidden cell has value 49591 but it is highlighted in B13 , how to avoid hidden cell ? can you advice !!
User generated image
No both formula your provided is not working
Ok, are you able to break the complex formula to few pieces and see which part is not working?

COUNTIF function will look for the total count regardless whether a cell is hidden or not.

if that's still not working, for ultimate solution would be using a macro to solve it.
Thank You Sir
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No its not working !!
can you illustrate what is not working? since you placed the macro codes in the Worksheet_Change event, you need to make a cell change to enable your worksheet to reflect with the changes.
Thank You sir , I got it , its working now.
I appreciate your time and effort to help me out.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You Formula what i required is  working now !!