Duplicate check with VBA code

jgopalk

New Member
Joined
Jun 14, 2011
Messages
26
Hi All,

I wanted to create <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help;">VBA</acronym> code for highlighting duplicate words in Sheet 2 with refer to sheet 1. Sheet 1 has master list. Sheet 2 has minimum values, which has to be compared and highlighted if any word found in sheet 1.

Example:
Sheet 1
Jasmine
Lotus
Lilly
Ladies finger
Carrot
Bird of Paradise

<tbody>
</tbody>


Sheet 2:
Jasmine
Ladies
Paradise
Blue bell
Jack fruit

<tbody>
</tbody>


After macro, answer should be:
Sheet 2:

Jasmine
Ladies
Paradise
Blue bell
Jack fruit

<tbody>
</tbody>



Note: Dupe does not need to be a exact one. For example, Master list has "Ladies finder", but in my sheet 2 i have added "Ladies" alone and it also should be highlighted.

Thanks in advance for your time in this.

Gopal​





 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this:

Code:
Sub Find_Duplicates()
Application.ScreenUpdating = False
'Modified 3-28-17 1:33 AM EDT
Dim i As Long
Sheets(1).Activate
Dim Lastrow As Long
Lastrow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        For b = 1 To Lastrowa
            If InStr(Cells(i, 1).Value, Sheets(2).Cells(b, 1).Value) Then
                Sheets(2).Cells(b, 1).Font.Color = vbRed
            End If
        Next
    Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
If you want the font red and Bold
Try this:

Code:
Sub Find_Duplicates()
Application.ScreenUpdating = False
'Modified 3-28-17 1:45 AM EDT
Dim i As Long
Sheets(1).Activate
Dim Lastrow As Long
Lastrow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        For b = 1 To Lastrowa
            If InStr(Cells(i, 1).Value, Sheets(2).Cells(b, 1).Value) Then
                Sheets(2).Cells(b, 1).Font.Color = vbRed
                Sheets(2).Cells(b, 1).Font.Bold = True
                
            End If
        Next
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is really great and thanks for your quick reply.

When I add "Paradise, London" in the "Sheet 2", that also should be highlighted in Red, which is not highlighting now. Duplicate need not to be a very exact one. If the "word" is in between the sentence also should be highlighted.

Hope I am clear.

Thanks,
Gopal
 
Upvote 0
That's not the way I understood your question.

In your image you showed a series on multiple words in sheet(1) and single values in sheet(2)

Someone else here at Mr. Excel will need to help you
 
Upvote 0
Yes, I missed to mention that. Sorry for that. When I checked your query, I realized that my question could have been little more clear.

Thanks for your support.

Someone kindly check for the possibility to fix this.

Gopal
 
Upvote 0
Thanks Dude.. I got it by modifying code. Thanks again for your help. Now the whole cell is colored for a specific word, but i need to have the specific word alone to be colored. I couldn't get that. Is there any way to get it?

Gopal
 
Upvote 0
Thanks Dude.. I got it by modifying code. Thanks again for your help. Now the whole cell is colored for a specific word, but i need to have the specific word alone to be colored. I couldn't get that. Is there any way to get it?

Gopal

Show me your script
 
Upvote 0
I'm not sure how you can do this.

If in sheet 1 you have:

This is my dog

And in sheet (2) you have:

This dog is Jane's dog

So the words "This" and "dog" and "is" are duplicates

So in sheet 2 all 3 words "dog" and "This" and "is" would need to be highlighted. And dog would have to be highlighted twice.

It will be interesting to see how this can be done.
I'm always trying to learn more myself.
 
Last edited:
Upvote 0
Below is the code which I tried out. It worked. I am not familiar with code. But, I just modified your code by referring google search.

Sub Find_Duplicates()
Application.ScreenUpdating = False
'Modified 3-28-17 1:33 AM EDT
Dim i As Long
Sheets(1).Activate
Dim Lastrow As Long
Lastrow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
For b = 1 To Lastrow
If InStr(1, Sheets(2).Cells(b, 1).Value, Cells(i, 1).Value) <> 0 Then
Sheets(2).Cells(b, 1).Font.Color = vbRed
End If
Next
Next
Application.ScreenUpdating = True
End Sub


Based on your example, I will have only the important keyword in sheet 1. For example, here I will have "dog" as a keyword in Sheet 1. So I want a result like "This dog is Jane's dog". Currently I am getting "This dog is Jane's dog" (whole cell colored).

Also currently code is case sensitive. But, I want it as "Case-insensitive".

Kindly check out. I am also trying it.

Thanks,
Gopal
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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