VBA to find matching results and highlight them.

Shwapx

New Member
Joined
Sep 28, 2022
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I'm looking to create a VBA which can search for matching results between 2 columns, for example, columns A and B, and highlights the results in A with yellow color, but the tricky part is to consider the first result as nonmatch.

So let's say I will have this example:

1668167670657.png


So basically comparing two columns, but highlighting only the ones which match and their count is more than 1.

At the moment I have created a macro that can highlight cells in column A, but it doesn't make the comparison between B and it might highlight cells that are not relevant.

Thanks in advance!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this:
VBA Code:
Sub Shwapx()
Dim i As Long
Dim c As Range
Dim v As String
Dim va
Dim d As Object

Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare

va = Range("B2", Cells(Rows.Count, "B").End(xlUp))

For i = 1 To UBound(va, 1)
    d(va(i, 1)) = Empty
Next

For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    v = c.Value
    If d.Exists(v) Then
        If d(v) = Empty Then
            d(v) = 1
        Else
            c.Interior.Color = vbYellow
        End If
    End If
Next
Application.ScreenUpdating = True

End Sub

Book3
AB
1
2GH
3AA
4HG
5G
6N
7H
8G
9M
10H
Sheet1
 
Upvote 0
Try this:
VBA Code:
Sub Shwapx()
Dim i As Long
Dim c As Range
Dim v As String
Dim va
Dim d As Object

Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare

va = Range("B2", Cells(Rows.Count, "B").End(xlUp))

For i = 1 To UBound(va, 1)
    d(va(i, 1)) = Empty
Next

For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    v = c.Value
    If d.Exists(v) Then
        If d(v) = Empty Then
            d(v) = 1
        Else
            c.Interior.Color = vbYellow
        End If
    End If
Next
Application.ScreenUpdating = True

End Sub

Book3
AB
1
2GH
3AA
4HG
5G
6N
7H
8G
9M
10H
Sheet1
Hello It's working on the example sheet, but on my sheet the values are actually numbers like that (231-2341-1) and I'm getting type mismatch error on For i = 1 To UBound(va, 1)
 
Upvote 0
I think it give me that result because i have only 1 value in COLUMN B
 
Upvote 0
I think I fix that by extending the macro to get the header as well for value (which will never have a duplicate like the header) so it seems a good solution. Thank you so much for this it's amazing how quickly to build it!
 
Upvote 0
I think it give me that result because i have only 1 value in COLUMN B

Try this one:
VBA Code:
Sub Shwapx2()
Dim i As Long
Dim c As Range
Dim v As String
Dim va, x
Dim d As Object

Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare

va = Range("B2", Cells(Rows.Count, "B").End(xlUp))

If Not IsArray(va) Then va = Array(va)
For Each x In va
    d(x) = Empty
Next

For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    v = c.Value
    If d.Exists(v) Then
        If d(v) = Empty Then
            d(v) = 1
        Else
            c.Interior.Color = vbYellow
        End If
    End If
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Try this one:
VBA Code:
Sub Shwapx2()
Dim i As Long
Dim c As Range
Dim v As String
Dim va, x
Dim d As Object

Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare

va = Range("B2", Cells(Rows.Count, "B").End(xlUp))

If Not IsArray(va) Then va = Array(va)
For Each x In va
    d(x) = Empty
Next

For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    v = c.Value
    If d.Exists(v) Then
        If d(v) = Empty Then
            d(v) = 1
        Else
            c.Interior.Color = vbYellow
        End If
    End If
Next
Application.ScreenUpdating = True

End Sub
Thanks it's working, what was causing that, it was working with adding the header with the previous code, but now works without header being considered.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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