VBA Highlight duplicates between two ranges

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
81
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone... I need some help again.

I need VBA code to highlight all duplicate numbers red between two ranges in each column separately. And then highlight all numbers blue according
to last duplicate number in that column. I made this manually for the first column to show this more clearly. See screenshot.
I also need to be able to adjust the ranges in VBA code... they are not always the same.

Does anyone have time and skill to write VBA code for this?

As always.
Thank you guys...
 

Attachments

  • Clipboard05.png
    Clipboard05.png
    76 KB · Views: 24
Quick question—why is 60 marked in red in your file? It doesn't seem to be a duplicate.

1743548387298.png
 
Upvote 0
try
Code:
Sub test()
    Dim c As Range, r As Range, myArea As Range, a, i&, ii&, myAreas As Areas, dic As Object
    Application.ScreenUpdating = False
    Set dic = CreateObject("Scripting.Dictionary")
    Set myAreas = Columns(2).SpecialCells(2, 1).Areas
    For Each myArea In myAreas
        With myArea.CurrentRegion
            .Interior.ColorIndex = xlNone
            a = .Value
            For ii = 1 To UBound(a, 2)
                For i = UBound(a, 1) To 1 Step -1
                    If a(i, ii) <> "" Then
                        If Not dic.exists(a(i, ii)) Then
                            If WorksheetFunction.CountIf(.Columns(ii), .Cells(i, ii)) > 1 Then
                                Set dic(a(i, ii)) = .Cells(i, ii)
                                If c Is Nothing Then
                                    Set c = .Cells(i, ii)
                                Else
                                    Set c = Union(.Cells(i, ii), c)
                                End If
                            End If
                        Else
                            If r Is Nothing Then
                                Set r = .Cells(i, ii)
                            Else
                                Set r = Union(.Cells(i, ii), r)
                            End If
                        End If
                    End If
                Next
                dic.RemoveAll
            Next
        End With
        If Not r Is Nothing Then
            r.Interior.Color = vbRed
            c.Interior.Color = vbBlue
        End If
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Test.xlsx
BCDEFGHIJK
2545325-282961-572853-57
3574520-25296.3-542545-54
4515531-143757-511445-51
538442.1-232858-512344-51
6524532-1.33755-501.345-50
7595225-273266-592752-59
8504626-203054-502046-50
9455927-153048-451542-45
10585024-263266-582650-58
11645335-203147-512055-51
12525030-203254-5220-52
13424624-22203846-42
14514733-14371447-51
15483332-1252-4812-48
165951253367-592651-59
175624-272955-562757-56
18545024-223258-542250-54
19345730-2729550.562757-56
20605222-303068-603052-60
21564724-2333650.5623
22405328-253153-56
23366124-20720.5820-58
24526330-252749-522555-52
25
26342924-52939-34529-34
27363124-72941-36731-36
28383324-92943-38933-38
29403524-112945-401135-40
30423724-132947-421337-42
31443924-152949-441539-44
32464124-172951-461741-46
33484324-192953-481943-48
34504524-212955-502145-50
35524724-232957-522347-52
36544924-252959-542549-54
37565124-272961-562751-56
38585324-292963-582953-58
39605524-312965-603155-60
40625724-332967-623357-62
41645924-352969-643559-64
42666124-372971-663761-66
43686324-392973-683963-68
Sheet 1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:K24Expression=B2=INDEX(B$2:B$24,MAX(IFERROR(SEQUENCE(ROWS($B$2:$K$24))/(COUNTIF(B$2:B$24,B$2:B$24)>1),"")))textNO
B2:K24Expression=AND(COUNTIF(B$2:B$43,B2)>1,COUNTIF(B$29:B$40,B2)>=1)textNO
 
Upvote 0
Maybe the below:
VBA Code:
Sub test()
    Dim rngSrch As Range, rngFind As Range, rCell As Range, rngC As Range
    Dim foundCell As Range, foundCells As Range, pCell As Range, lastDup As Range
    Dim firstAddress As String
    Dim lcl As Long
    
    Set rngSrch = Range("A2:J24") ' Range to change the colour of
    Set rngFind = Range("A29:J40") ' Range containing the values to find
    
    Application.ScreenUpdating = False
    For Each rngC In rngSrch.Columns
        For Each rCell In rngFind.Columns(rngC.Column).Cells
            Set foundCell = rngC.Find(rCell.Value, rngC.Cells(1, 1), , xlWhole)
            If Not foundCell Is Nothing Then
                firstAddress = foundCell.Address
                Set foundCells = foundCell
                Do
                    Set foundCell = rngC.FindNext(foundCell)
                    If Not foundCell Is Nothing And foundCell.Address <> firstAddress Then
                        Set foundCells = Union(foundCells, foundCell)
                    Else
                        Exit Do
                    End If
                Loop
                For Each pCell In foundCells
                    If pCell.Row > lcl Then lcl = pCell.Row: Set lastDup = foundCells
                Next pCell
                foundCells.Interior.Color = vbRed
            End If
        Next rCell
        If Not lastDup Is Nothing Then lastDup.Interior.Color = vbBlue
        Set lastDup = Nothing
        lcl = 0
    Next rngC
    Application.ScreenUpdating = True
End Sub

Book1
ABCDEFGHIJK
1
254545454545454545454
357575757575757575757
451515151515151515151
538383838383838383838
652525252525252525252
759595959594059595959
850505050505050505050
945454545454545454545
1058585858585858585858
1164646464646464646464
1252525252525252525252
1342424242424242424242
1451515151514051515151
1548484848484848484848
1659595959595959595959
1756565656565656565656
1854545454545454545454
1934343434343434343434
2060606060604060606060
2156565656565656565656
2240404040404040404040
2336363636363636363636
2452525252525252525252
25
2634343434343434343434
2736363636363636363636
2838383838383838383838
294040100100100100100100100100
30424242421004242424242
31444444441004444444444
32464646461004646464646
33484848481003434343434
34505050501005050505050
35529999991004059994059
3654200542001005454545454
37565656101100101101101101101
38585858581005858585858
3960606060100102102102102102
40626262621006262626262
4164646464646464646464
4266666666666666666666
4368686868686868686868
4468686868686868686868
45
Sheet1
 
Upvote 0
Maybe the below:
VBA Code:
Sub test()
    Dim rngSrch As Range, rngFind As Range, rCell As Range, rngC As Range
    Dim foundCell As Range, foundCells As Range, pCell As Range, lastDup As Range
    Dim firstAddress As String
    Dim lcl As Long
   
    Set rngSrch = Range("A2:J24") ' Range to change the colour of
    Set rngFind = Range("A29:J40") ' Range containing the values to find
   
    Application.ScreenUpdating = False
    For Each rngC In rngSrch.Columns
        For Each rCell In rngFind.Columns(rngC.Column).Cells
            Set foundCell = rngC.Find(rCell.Value, rngC.Cells(1, 1), , xlWhole)
            If Not foundCell Is Nothing Then
                firstAddress = foundCell.Address
                Set foundCells = foundCell
                Do
                    Set foundCell = rngC.FindNext(foundCell)
                    If Not foundCell Is Nothing And foundCell.Address <> firstAddress Then
                        Set foundCells = Union(foundCells, foundCell)
                    Else
                        Exit Do
                    End If
                Loop
                For Each pCell In foundCells
                    If pCell.Row > lcl Then lcl = pCell.Row: Set lastDup = foundCells
                Next pCell
                foundCells.Interior.Color = vbRed
            End If
        Next rCell
        If Not lastDup Is Nothing Then lastDup.Interior.Color = vbBlue
        Set lastDup = Nothing
        lcl = 0
    Next rngC
    Application.ScreenUpdating = True
End Sub

Book1
ABCDEFGHIJK
1
254545454545454545454
357575757575757575757
451515151515151515151
538383838383838383838
652525252525252525252
759595959594059595959
850505050505050505050
945454545454545454545
1058585858585858585858
1164646464646464646464
1252525252525252525252
1342424242424242424242
1451515151514051515151
1548484848484848484848
1659595959595959595959
1756565656565656565656
1854545454545454545454
1934343434343434343434
2060606060604060606060
2156565656565656565656
2240404040404040404040
2336363636363636363636
2452525252525252525252
25
2634343434343434343434
2736363636363636363636
2838383838383838383838
294040100100100100100100100100
30424242421004242424242
31444444441004444444444
32464646461004646464646
33484848481003434343434
34505050501005050505050
35529999991004059994059
3654200542001005454545454
37565656101100101101101101101
38585858581005858585858
3960606060100102102102102102
40626262621006262626262
4164646464646464646464
4266666666666666666666
4368686868686868686868
4468686868686868686868
45
Sheet1
Hey man:)

Thanks for this VBA code... It works fine until i change the ranges.
I uploaded worksheet with different ranges... Try it yourself just change the ranges in code and it wont work correctly anymore...

Can you fix this?

Correct ranges for worksheet i uploaded are B2:K24 and B29:K40 right? Or im doing something wrong?
 
Upvote 0
This is what I came up with
VBA Code:
Sub RedBlue()
    Dim a As Variant, b As Variant
    Dim rSrch As Range, rFind As Range, rRed As Range, rBlue As Range, rLast As Range
    Dim i As Long, j As Long, k As Long, rws As Long, ubb As Long
    
    Set rSrch = Range("B2:K24") ' Range to change the colour of
    Set rFind = Range("B29:K40") ' Range containing the values to find
    
    b = rFind.Value
    ubb = UBound(b)
    With rSrch
      .Interior.Color = xlNone
      a = .Value
      rws = UBound(a)
      Set rRed = .Cells(.Rows.Count + 1, 1)
      Set rBlue = .Cells(.Rows.Count + 1, 1)
      For j = 1 To UBound(a, 2)
        Set rLast = Nothing
        For i = rws To 1 Step -1
          For k = 1 To ubb
            If a(i, j) = b(k, j) Then
              If rLast Is Nothing Then Set rLast = .Cells(i, j)
              If a(i, j) = rLast.Value Then
                Set rBlue = Union(rBlue, .Cells(i, j))
              Else
                Set rRed = Union(rRed, .Cells(i, j))
              End If
            End If
          Next k
        Next i
      Next j
      rRed.Interior.Color = vbRed
      rBlue.Interior.Color = vbBlue
      .Cells(.Rows.Count + 1, 1).Interior.Color = xlNone
    End With
End Sub


My results:

Yamasaki450.xlsm
BCDEFGHIJK
1
2545325-282961-572853-57
3574520-252963-542545-54
4515531-143757-511445-51
5384421-232858-512344-51
6524532-133755-501345-50
7595225-273266-592752-59
8504626-203054-502046-50
9455927-153048-451542-45
10585024-263266-582650-58
11645335-203147-512055-51
12525030-203254-522050-52
13424624-222038-422246-42
14514733-143755-511447-51
15483332-123652-481244-48
16595125-263367-592651-59
17565730-272955-562757-56
18545028-223258-542250-54
19345730-272955-562757-56
20605222-303068-603052-60
21564724-233365-562347-56
22405328-253159-562553-56
23366124-203872-582044-58
24526330-252749-522555-52
25
26342924-52939-34529-34
27363124-72941-36731-36
28383324-92943-38933-38
29403524-112945-401135-40
30423724-132947-421337-42
31443924-152949-441539-44
32464124-172951-461741-46
33484324-192953-481943-48
34504524-212955-502145-50
35524724-232957-522347-52
36544924-252959-542549-54
37565124-272961-562751-56
38585324-292963-582953-58
39605524-312965-603155-60
40625724-332967-623357-62
41645924-352969-643559-64
42666124-372971-663761-66
43686324-392973-683963-68
Sheet1 (3)
 
Upvote 0
Here is the fixed code:
VBA Code:
Sub test()
    Dim rngSrch As Range, rngFind As Range, rCell As Range, rngC As Range
    Dim foundCell As Range, foundCells As Range, pCell As Range, lastDup As Range
    Dim firstAddress As String
    Dim lcl As Long
    
    Set rngSrch = Range("B2:K24") ' Range to change the colour of
    Set rngFind = Range("B29:K40") ' Range containing the values to find
    
    Application.ScreenUpdating = False
    For Each rngC In rngSrch.Columns
        For Each rCell In rngFind.Columns(rngC.Column - rngSrch.Column + 1).Cells
            Set foundCell = rngC.Find(rCell.Value, rngC.Cells(1, 1), , xlWhole)
            If Not foundCell Is Nothing Then
                firstAddress = foundCell.Address
                Set foundCells = foundCell
                Do
                    Set foundCell = rngC.FindNext(foundCell)
                    If Not foundCell Is Nothing And foundCell.Address <> firstAddress Then
                        Set foundCells = Union(foundCells, foundCell)
                    Else
                        Exit Do
                    End If
                Loop
                For Each pCell In foundCells
                    If pCell.Row > lcl Then lcl = pCell.Row: Set lastDup = foundCells
                Next pCell
                foundCells.Interior.Color = vbRed
            End If
        Next rCell
        If Not lastDup Is Nothing Then lastDup.Interior.Color = vbBlue
        Set lastDup = Nothing
        lcl = 0
    Next rngC
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This is what I came up with
VBA Code:
Sub RedBlue()
    Dim a As Variant, b As Variant
    Dim rSrch As Range, rFind As Range, rRed As Range, rBlue As Range, rLast As Range
    Dim i As Long, j As Long, k As Long, rws As Long, ubb As Long
   
    Set rSrch = Range("B2:K24") ' Range to change the colour of
    Set rFind = Range("B29:K40") ' Range containing the values to find
   
    b = rFind.Value
    ubb = UBound(b)
    With rSrch
      .Interior.Color = xlNone
      a = .Value
      rws = UBound(a)
      Set rRed = .Cells(.Rows.Count + 1, 1)
      Set rBlue = .Cells(.Rows.Count + 1, 1)
      For j = 1 To UBound(a, 2)
        Set rLast = Nothing
        For i = rws To 1 Step -1
          For k = 1 To ubb
            If a(i, j) = b(k, j) Then
              If rLast Is Nothing Then Set rLast = .Cells(i, j)
              If a(i, j) = rLast.Value Then
                Set rBlue = Union(rBlue, .Cells(i, j))
              Else
                Set rRed = Union(rRed, .Cells(i, j))
              End If
            End If
          Next k
        Next i
      Next j
      rRed.Interior.Color = vbRed
      rBlue.Interior.Color = vbBlue
      .Cells(.Rows.Count + 1, 1).Interior.Color = xlNone
    End With
End Sub


My results:

Yamasaki450.xlsm
BCDEFGHIJK
1
2545325-282961-572853-57
3574520-252963-542545-54
4515531-143757-511445-51
5384421-232858-512344-51
6524532-133755-501345-50
7595225-273266-592752-59
8504626-203054-502046-50
9455927-153048-451542-45
10585024-263266-582650-58
11645335-203147-512055-51
12525030-203254-522050-52
13424624-222038-422246-42
14514733-143755-511447-51
15483332-123652-481244-48
16595125-263367-592651-59
17565730-272955-562757-56
18545028-223258-542250-54
19345730-272955-562757-56
20605222-303068-603052-60
21564724-233365-562347-56
22405328-253159-562553-56
23366124-203872-582044-58
24526330-252749-522555-52
25
26342924-52939-34529-34
27363124-72941-36731-36
28383324-92943-38933-38
29403524-112945-401135-40
30423724-132947-421337-42
31443924-152949-441539-44
32464124-172951-461741-46
33484324-192953-481943-48
34504524-212955-502145-50
35524724-232957-522347-52
36544924-252959-542549-54
37565124-272961-562751-56
38585324-292963-582953-58
39605524-312965-603155-60
40625724-332967-623357-62
41645924-352969-643559-64
42666124-372971-663761-66
43686324-392973-683963-68
Sheet1 (3)
Here is the fixed code:
VBA Code:
Sub test()
    Dim rngSrch As Range, rngFind As Range, rCell As Range, rngC As Range
    Dim foundCell As Range, foundCells As Range, pCell As Range, lastDup As Range
    Dim firstAddress As String
    Dim lcl As Long
   
    Set rngSrch = Range("B2:K24") ' Range to change the colour of
    Set rngFind = Range("B29:K40") ' Range containing the values to find
   
    Application.ScreenUpdating = False
    For Each rngC In rngSrch.Columns
        For Each rCell In rngFind.Columns(rngC.Column - rngSrch.Column + 1).Cells
            Set foundCell = rngC.Find(rCell.Value, rngC.Cells(1, 1), , xlWhole)
            If Not foundCell Is Nothing Then
                firstAddress = foundCell.Address
                Set foundCells = foundCell
                Do
                    Set foundCell = rngC.FindNext(foundCell)
                    If Not foundCell Is Nothing And foundCell.Address <> firstAddress Then
                        Set foundCells = Union(foundCells, foundCell)
                    Else
                        Exit Do
                    End If
                Loop
                For Each pCell In foundCells
                    If pCell.Row > lcl Then lcl = pCell.Row: Set lastDup = foundCells
                Next pCell
                foundCells.Interior.Color = vbRed
            End If
        Next rCell
        If Not lastDup Is Nothing Then lastDup.Interior.Color = vbBlue
        Set lastDup = Nothing
        lcl = 0
    Next rngC
    Application.ScreenUpdating = True
End Sub
What can i say guys. Hats off. Both codes work but the one that Georgiboy posted is much faster when it comes to larger amount of data.
Thanks to both of you. Cant thank you enough.

Have a nice day.
 
Upvote 0
Test.xlsx
BCDEFGHIJK
2545325-282961-572853-57
3574520-25296.3-542545-54
4515531-143757-511445-51
538442.1-232858-512344-51
6524532-1.33755-501.345-50
7595225-273266-592752-59
8504626-203054-502046-50
9455927-153048-451542-45
10585024-263266-582650-58
11645335-203147-512055-51
12525030-203254-5220-52
13424624-22203846-42
14514733-14371447-51
15483332-1252-4812-48
165951253367-592651-59
175624-272955-562757-56
18545024-223258-542250-54
19345730-2729550.562757-56
20605222-303068-603052-60
21564724-2333650.5623
22405328-253153-56
23366124-20720.5820-58
24526330-252749-522555-52
25
26342924-52939-34529-34
27363124-72941-36731-36
28383324-92943-38933-38
29403524-112945-401135-40
30423724-132947-421337-42
31443924-152949-441539-44
32464124-172951-461741-46
33484324-192953-481943-48
34504524-212955-502145-50
35524724-232957-522347-52
36544924-252959-542549-54
37565124-272961-562751-56
38585324-292963-582953-58
39605524-312965-603155-60
40625724-332967-623357-62
41645924-352969-643559-64
42666124-372971-663761-66
43686324-392973-683963-68
Sheet 1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:K24Expression=B2=INDEX(B$2:B$24,MAX(IFERROR(SEQUENCE(ROWS($B$2:$K$24))/(COUNTIF(B$2:B$24,B$2:B$24)>1),"")))textNO
B2:K24Expression=AND(COUNTIF(B$2:B$43,B2)>1,COUNTIF(B$29:B$40,B2)>=1)textNO
This also works but its easier to change ranges in VBA code...
Thanks to you to.
 
Upvote 0

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