Highlight matches between 2 columns of delimited values

dougebowl

Board Regular
Joined
Feb 22, 2010
Messages
60
First off, a huge thank you for any assistance that can be provided.

I searched the forums and found the code below to highlight values which match (seems to be single values in each column). How could this be modified to compare delimited values to highlight the matching values between the 2 columns. Data will always be a single value or multiple values delimited with a semi-colon ";". I am looking to determine if there are 'conflicting' values, so the code only needs to analyze cells in column C which contain a semi-colon (if there is only one value, there cannot be a conflict).

I would like the matching values to be highlighted in Red (instead of Green). I have included a screen shot of my data. Basically, column E contains the conflicting values, and for column C, if there are matching values to 2 or more of the values in column E, color code the values in Red font color.

Data: The number of rows in Column C and Column E can be different every time I need to do the comparison, so need the code to determine the last row for both columns to compare all the rows.

Sample Data.jpg


Sub Nanditha()
Dim Cl As Range
Dim Dic As Object

Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = 1
With Sheets("dnd")
For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
Dic(Cl.Value) = Empty
Next Cl
End With
With Sheets("cc5")
For Each Cl In .Range("P2", .Range("P" & Rows.Count).End(xlUp))
If Dic.Exists(Cl.Value) Then Cl.Font.Color = rgbLimeGreen
Next Cl
End With
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
It is not really clear.

You want to compare if value in column C existed in column E and highlight matching in column E?
For row 3 in column C, the whole phrase need to match or separate into two phrases?

Like this for one example?
 

Attachments

  • Sample Image.png
    Sample Image.png
    30.4 KB · Views: 32
Upvote 0
It is not really clear.

You want to compare if value in column C existed in column E and highlight matching in column E?
For row 3 in column C, the whole phrase need to match or separate into two phrases?

Like this for one example?
I need to compare the individual values in column C to those in column E. If there are two values that match any two values in column E. Column E is the list of ‘conflicting’ combinations of values. Therefore, I need those conflicting values highlighted in column C. Please let me know if there are further questions.
 
Upvote 0
In sample below A,B as individual value or A and B are separate values?

How would you highlight the sample below?

Sample Data Zaki.xlsm
ABC
1AA,D
2A,BA,C
3CD
4DF
5D,FC
Sheet2
 
Upvote 0
Try:
VBA Code:
Sub test()
Application.ScreenUpdating = False
Dim s As Variant
LrA = Cells(Rows.Count, "A").End(xlUp).Row
LrE = Cells(Rows.Count, "E").End(xlUp).Row
Range("A2:A" & LrA).Interior.Color = xlNone
    For Each A In Range("A2:A" & LrA)
        s = Split(A, ";")
        If Not IsEmpty(s) Then
            For Each E In Range("E2:E" & LrE)
            k = 0
                For i = 0 To UBound(s)
                    If InStr(1, E, s(i)) > 0 Then k = k + 1
                Next
                    If k > 1 Then A.Interior.Color = 255
            Next
        End If
    Next
Application.ScreenUpdating = True
End Sub
 

Attachments

  • Screenshot 2021-12-02 120009.png
    Screenshot 2021-12-02 120009.png
    42 KB · Views: 27
Upvote 0
In sample below A,B as individual value or A and B are separate values?

How would you highlight the sample below?

Sample Data Zaki.xlsm
ABC
1AA,D
2A,BA,C
3CD
4DF
5D,FC
Sheet2
Based on your sample, I realize I might not have been 100% clear in what I need highlighted. I need to highlight if two or more values in column A are identified in column C. Column C will always contain less rows than column A as column C identifies all the conflict combinations that column A needs to be compared against.

Value(s)Explanation of HighlightConflicts
A;B;CA and B match the combination in C4A;D;K;W;Z
AOnly one value in column A, so there will not be a conflictB;C
BOnly one value in column A, so there will not be a conflictA;B
A;D;K;T;SA, D, and K match the combination in C2B
D;E;FOnly D appears in the conflicts column, but, no additional values match. No highlight
Highlight the combination of values in column A, when two or more of the values match the values in column C
 
Upvote 0
In cell A2, why isn't the "C" red as well since B and C in A2 match the combination in C3?

If A7 was "B;C;K;Z" what, if anything, would get coloured red given that B and C are in C3 and also K & Z are in C2?
 
Upvote 0
try:
VBA Code:
Sub Color()
Dim spL1 As Variant
Dim a, e As Range
Dim LrA, LrE, i, k As Long
LrA = Range("A65000").End(xlUp).Row
LrE = Range("E65000").End(xlUp).Row
Range("A:A").Font.Color = 0
For Each a In Range("A2:A" & LrA)
    spL1 = Split(a, ";")
    For Each e In Range("E2:E" & LrE)
        k = 0
        For i = 0 To UBound(spL1) ' loop through each name in column A
        Start = InStr(1, a, spL1(i))
        Lenght = Len(spL1(i))
            If InStr(1, e, spL1(i)) > 0 Then ' check if each name exists
            a.Characters(Start, Lenght).Font.Color = 255
            k = k + 1
            End If
        Next
            If k >= 2 Then GoTo Z
            If k < 2 Then a.Font.Color = 0
    Next
Z:
Next
End Sub

Book2.xlsm
ABCDE
1
2NameA;NameX;NameCNameA;NameD;NameB;NameW;NameZ;NameC
3NameANameB;NameC
4NameBNameA;NameB
5NameA;NameD;NameK;NameT;NameSNameB;NameC
6NameD;NameA;NameZ
Sheet1
 

Attachments

  • Screenshot 2021-12-03 153858.png
    Screenshot 2021-12-03 153858.png
    17.9 KB · Views: 18
Upvote 0
In cell A2, why isn't the "C" red as well since B and C in A2 match the combination in C3?

If A7 was "B;C;K;Z" what, if anything, would get coloured red given that B and C are in C3 and also K & Z are in C2?
You are 100% correct, I obviously was moving too fast. Whenever there are 2 values in column A where those combinations are identified in Column C, they should highlight red. I will give the code you provided a try and let you know if we have a solution. THANK YOU VERY MUCH!!!
 
Upvote 0
I will give the code you provided a try
I didn't provide any code. ;)

.. but I will now. :)

VBA Code:
Sub MarkConflicts()
  Dim RX As Object, M As Object
  Dim ColA As Variant, ColC As Variant
  Dim i As Long, j As Long, k As Long, ubC As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  ColC = Range("C2", Range("C" & Rows.Count).End(xlUp)).Value
  ubC = UBound(ColC)
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    ColA = .Value
    For i = 1 To .Count
      RX.Pattern = ";" & Replace(ColA(i, 1), ";", ";|;") & ";"
      For j = 1 To ubC
        Set M = RX.Execute(";" & Replace(ColC(j, 1), ";", ";;") & ";")
        If M.Count > 1 Then
          For k = 1 To M.Count
            .Cells(i).Characters(InStr(1, ";" & .Cells(i).Text & ";", M(k - 1)), Len(M(k - 1)) - 2).Font.Color = vbRed
          Next k
        End If
      Next j
    Next i
  End With
End Sub

Here are my sample data and results. I have manually entered what I think are the reasons for highlighting.

1638578876992.png
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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