Color duplicated cell to an interior colour

mmldogs

New Member
Joined
Jul 31, 2019
Messages
5
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello
I have found code that identifies a duplicate in a single column . Instead of writing " duplicate' in column 2 when duplicate found I would like the identified duplicate higlighted in yellow in column 1
Newbee to vba got FALSE as value and compiling error

Can someone assist




VBA Code:
Sub sbFindDuplicatesInColumn()
    Dim lastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    lastRow = Range("A65000").End(xlUp).Row

    For iCntr = 1 To lastRow
    If Cells(iCntr, 1) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
        If iCntr <> matchFoundIndex Then
            Cells(iCntr, 2) = "Duplicate"
       End If
    End If
    Next
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Like this?

VBA Code:
Sub sbFindDuplicatesInColumn()
    Dim lastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    lastRow = Range("A65000").End(xlUp).Row

    For iCntr = 1 To lastRow
    If Cells(iCntr, 1) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
        If iCntr <> matchFoundIndex Then
            Cells(iCntr, 1).Interior.Color = 65535
       End If
    End If
    Next
End Sub
 
Upvote 0
Welcome to the MrExcel board!

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Try this with a copy of your workbook to see if it suits your purpose.

VBA Code:
Sub Find_Dupes()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .FormatConditions.Add Type:=xlExpression, Formula1:="=MATCH(A2,A$1:A1,0)"
    .FormatConditions(1).Interior.Color = vbYellow
  End With
End Sub
 
Upvote 0
hey thanks for the prompt reply
works well for column A . If I were to apply this to a range in column E
Would the code be ?

trying to understand and apply .
All profile updated
VBA Code:
Sub Find_Dupes()
  With Range("E3", Range("E" & Rows.Count).End(xlUp))
    .FormatConditions.Add Type:=xlExpression, Formula1:="=MATCH(E3,E$3:E3,0)"   ' or  zero replace 0 with 5 because its the fifth column 
    .FormatConditions(1).Interior.Color = vbYellow
  End With
End Sub
 
Upvote 0
If I were to apply this to a range in column E
Would the code be ?
Rich (BB code):
With Range("E3", Range("E" & Rows.Count).End(xlUp))
The blue should be the second cell of the actual data you are trying to check for duplicates. Logic is that you cannot get a duplicate with the first value so no need to do anything with that cell. So E3 would be correct if you had a heading in row 1 and data started in row 2.

Rich (BB code):
Formula1:="=MATCH(E3,E$3:E3,0)"
This is not correct. Those red 3s should be 1 less than the blue value. Logic is that we are checking above the 'current' cell to see if that value already exists. Note that in my original code it was:
Rich (BB code):
Formula1:="=MATCH(A2,A$1:A1,0)"


.. and thanks for updating your profile. (y)
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,039
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top