Highlighting duplicates in one column based on the data from other column

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hey guys,

Please see my sample data below. The data could be in any columns.

[TABLE="class: grid, width: 200, align: left"]
<tbody>[TR]
[TD]1[/TD]
[TD]a[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]c[/TD]
[/TR]
</tbody>[/TABLE]
I want to have a macro that highlight (using interior color) the data in the first column (any column) based on the duplicates data from the other column (any column). The macro should work in following steps.

After selecting the data in first column I run the macro.
I enter the column number for the column having duplicate entries, in an inputbox
The macro generate the following output

[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD]1[/TD]
[TD]a[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]3 red[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]4 red[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]6 red[/TD]
[TD]c[/TD]
[/TR]
</tbody>[/TABLE]

I hope my requirement is clear. Thank you in advance.
 
Here is code that I think will do what you wanted back on your original conditions.
Note that if the column you are checking for duplicates is always one column to the right of the selected column, then you can get rid of the Input Box altogether. Just delete that line, and uncomment the line just below it.
Code:
Sub MyFormatMacro()

    Dim lr As Long
    Dim r As Long
    Dim col As Long
    Dim col2 As Variant
    Dim rng As Range
    
    Application.ScreenUpdating = True
    
'   Capture column of activecell
    col = ActiveCell.Column
    
'   Prompt for column to check for duplicates
    col2 = InputBox("Please enter column letter you wish to check for duplicates")
    'col2 = col + 1
    
'   Find last row in 2nd column
    lr = Cells(Rows.Count, col2).End(xlUp).Row
    
'   Loop through each row
    For r = 1 To lr
'       Set range for formula
        Set rng = Range(Cells(1, col2), Cells(r, col2))
'       Check for duplicates
        If Application.WorksheetFunction.CountIf(rng, Cells(r, col2).Value) > 1 Then
'           Color cell red
            Cells(r, col).Interior.Color = 255
        End If
    Next r

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Here is code that I think will do what you wanted back on your original conditions.
Note that if the column you are checking for duplicates is always one column to the right of the selected column, then you can get rid of the Input Box altogether. Just delete that line, and uncomment the line just below it.

Thank you Joe4. It works well. Have a good day!
 
Upvote 0
Here is code that I think will do what you wanted back on your original conditions.
Note that if the column you are checking for duplicates is always one column to the right of the selected column, then you can get rid of the Input Box altogether. Just delete that line, and uncomment the line just below it.
Code:
Sub MyFormatMacro()

    Dim lr As Long
    Dim r As Long
    Dim col As Long
    Dim col2 As Variant
    Dim rng As Range
    
    Application.ScreenUpdating = True
    
'   Capture column of activecell
    col = ActiveCell.Column
    
'   Prompt for column to check for duplicates
    col2 = InputBox("Please enter column letter you wish to check for duplicates")
    'col2 = col + 1
    
'   Find last row in 2nd column
    lr = Cells(Rows.Count, col2).End(xlUp).Row
    
'   Loop through each row
    For r = 1 To lr
'       Set range for formula
        Set rng = Range(Cells(1, col2), Cells(r, col2))
'       Check for duplicates
        If Application.WorksheetFunction.CountIf(rng, Cells(r, col2).Value) > 1 Then
'           Color cell red
            Cells(r, col).Interior.Color = 255
        End If
    Next r

    Application.ScreenUpdating = True

End Sub

hi Joe4,

Sorry to disturb you again.

I want to modify this code so the I can enter (in the inputbox) the column number of second column instead of column letter. Could you please help me with that. Thank you.
 
Upvote 0
Simply change the variable declaration to long, i.e.
Code:
    Dim col2 As Long
and you can change the wording of your InputBox to anything you like.
 
Upvote 0
Hey , joey i too have some doubts regarding macro in excel but i am not able to raise a thread or post it . Can you help me with that ?
 
Upvote 0

Forum statistics

Threads
1,225,138
Messages
6,183,089
Members
453,147
Latest member
Bree2019

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