How to compare delimited values regardless of order

dougebowl

Board Regular
Joined
Feb 22, 2010
Messages
60
I need to compare delimited values in two columns to determine if the values all the values are present in the adjacent cell for each row.
  1. The two columns are not static, so I need to be able to select the columns to compare
  2. Values in either column could be duplicated in a single cell, so looking to insure each value in the cell is contained in the adjacent cell
  3. Values could appear in different order, so a simple match formula does not work
  4. For value(s) in the cell that are not found in the adjacent cell, I would like the font color to be "Red"
  5. Comparison should look column 1 compared to column 2 and column 2 compared to column 1 to display the differences
  6. Values will always be semi-colon ";" delimited
I hope this ask is fairly straight forward and clear. I do so appreciate the assistance.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This is the kind of question that would benefit greatly from some sample data, not just an explanation. Here's what I came up with:

Book1 (version 1).xlsb
ABCDEF
1List1List3List1List2List3List4
2cat;doghen;catdog;bananahen;turtle
3mouse;lizarddog;mousemouse;housedog;mouse
4pig;henpeach;catlizard;mousehouse;mouse
5turtle;bananabanana;peachlizard;turtle
6hen;cat
7
Sheet8
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C:FExpression=AND(ROW()>1,C1<>"",OR(COUNTIF(INDEX($C$2:$F$20,0,MATCH(INDEX($A$1:$B$1,3-MATCH(C$1,$A$1:$B$1,0)),$C$1:$F$1,0)),"*"&LEFT(C1,FIND(";",C1)-1)&"*")=0,(COUNTIF(INDEX($C$2:$F$20,0,MATCH(INDEX($A$1:$B$1,3-MATCH(C$1,$A$1:$B$1,0)),$C$1:$F$1,0)),"*"&MID(C1,FIND(";",C1)+1,99)&"*")=0)))textNO


Put the lists you want to compare in A1:B1. This formula is highly dependent on the sheet layout, but might be improved with an example.
 
Upvote 0
1631568907730.png



VBA Code:
Sub ColorRedNonMatch()  'Select two column range to compare columns with semicolon delimited data, limited to 25 non-matched items per cell
    Dim A, Rng As Range, uba As Long, i As Long, j As Long, item1, item2, k As Long, lencount As Long, found As Boolean
    Set Rng = Application.InputBox("Select Range", Default:=Selection.Address, Type:=8)
    A = Rng
    uba = UBound(A)
    ReDim B(1 To uba, 1 To 51)
    ReDim C(1 To uba, 1 To 51)
    For i = 1 To uba
        For j = 1 To 2
            A(i, j) = Split(A(i, j), ";")
        Next
        
        k = 2
        lencount = 0
        For Each item1 In A(i, 1)
            found = False
            For Each item2 In A(i, 2)
                If item1 = item2 Then
                    found = True
                    lencount = lencount + Len(item1) + 1
                    Exit For
                End If
            Next
            If Not found Then
                B(i, 1) = B(i, 1) + 1
                B(i, k) = lencount + 1
                B(i, k + 1) = Len(item1)
                lencount = lencount + Len(item1) + 1
                k = k + 2
            End If
        Next
    Next
    
    For i = 1 To uba
        k = 2
        lencount = 0
        For Each item1 In A(i, 2)
            found = False
            For Each item2 In A(i, 1)
                If item1 = item2 Then
                    found = True
                    lencount = lencount + Len(item1) + 1
                    Exit For
                End If
            Next
            If Not found Then
                C(i, 1) = C(i, 1) + 1
                C(i, k) = lencount + 1
                C(i, k + 1) = Len(item1)
                lencount = lencount + Len(item1) + 1
                k = k + 2
            End If
        Next
    Next
    
    For i = 1 To uba
        For j = 1 To B(i, 1)
            Rng(i, 1).Characters(Start:=B(i, j * 2), Length:=B(i, j * 2 + 1)).Font.Color = -16776961
        Next
        For j = 1 To C(i, 1)
            Rng(i, 2).Characters(Start:=C(i, j * 2), Length:=C(i, j * 2 + 1)).Font.Color = -16776961
        Next
    Next

End Sub
 
Upvote 0
View attachment 46825


VBA Code:
Sub ColorRedNonMatch()  'Select two column range to compare columns with semicolon delimited data, limited to 25 non-matched items per cell
    Dim A, Rng As Range, uba As Long, i As Long, j As Long, item1, item2, k As Long, lencount As Long, found As Boolean
    Set Rng = Application.InputBox("Select Range", Default:=Selection.Address, Type:=8)
    A = Rng
    uba = UBound(A)
    ReDim B(1 To uba, 1 To 51)
    ReDim C(1 To uba, 1 To 51)
    For i = 1 To uba
        For j = 1 To 2
            A(i, j) = Split(A(i, j), ";")
        Next
       
        k = 2
        lencount = 0
        For Each item1 In A(i, 1)
            found = False
            For Each item2 In A(i, 2)
                If item1 = item2 Then
                    found = True
                    lencount = lencount + Len(item1) + 1
                    Exit For
                End If
            Next
            If Not found Then
                B(i, 1) = B(i, 1) + 1
                B(i, k) = lencount + 1
                B(i, k + 1) = Len(item1)
                lencount = lencount + Len(item1) + 1
                k = k + 2
            End If
        Next
    Next
   
    For i = 1 To uba
        k = 2
        lencount = 0
        For Each item1 In A(i, 2)
            found = False
            For Each item2 In A(i, 1)
                If item1 = item2 Then
                    found = True
                    lencount = lencount + Len(item1) + 1
                    Exit For
                End If
            Next
            If Not found Then
                C(i, 1) = C(i, 1) + 1
                C(i, k) = lencount + 1
                C(i, k + 1) = Len(item1)
                lencount = lencount + Len(item1) + 1
                k = k + 2
            End If
        Next
    Next
   
    For i = 1 To uba
        For j = 1 To B(i, 1)
            Rng(i, 1).Characters(Start:=B(i, j * 2), Length:=B(i, j * 2 + 1)).Font.Color = -16776961
        Next
        For j = 1 To C(i, 1)
            Rng(i, 2).Characters(Start:=C(i, j * 2), Length:=C(i, j * 2 + 1)).Font.Color = -16776961
        Next
    Next

End Sub
JGordon, this is exactly what I was looking for. THANK you so much, you have saved me hours of manually comparing columns of data.
 
Upvote 0
Glad it's working for you. Thanks for the feedback.
 
Upvote 0
I ran into an issue where I have more than 25 values needing to be matched. The comparison does not execute for these. Is there a way to modify this so there is no limitation to the number of values?
 
Upvote 0
Although I used the same setup as JGordon11 did, this macro uses a completely different (much more compact) approach than he used. And while I did not test for it, I see no reason it should have any size limitations. See if it works for you...
VBA Code:
Sub HighlightUniques()
  Dim Cell As Range, V As Variant, Acol As Variant, Bcol As Variant
  For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
    Cell.Font.Color = vbBlack
    Cell.Offset(, 1).Font.Color = vbBlack
    Acol = Split(Cell.Value, ";")
    Bcol = Split(Cell.Offset(, 1).Value, ";")
    For Each V In Acol
      If UBound(Filter(Bcol, V, True)) = -1 Then Cell.Characters(InStr(";" & Cell.Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
    For Each V In Bcol
      If UBound(Filter(Acol, V, True)) = -1 Then Cell.Offset(, 1).Characters(InStr(";" & Cell.Offset(, 1).Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
  Next
End Sub
 
Last edited:
Upvote 0
Although I used the same setup as JGordon11 did, this macro uses a completely different (much more compact) approach than he used. And while I did not test for it, I see no reason it should have any size limitations. See if it works for you...
VBA Code:
Sub HighlightUniques()
  Dim Cell As Range, V As Variant, Acol As Variant, Bcol As Variant
  For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
    Cell.Font.Color = vbBlack
    Cell.Offset(, 1).Font.Color = vbBlack
    Acol = Split(Cell.Value, ";")
    Bcol = Split(Cell.Offset(, 1).Value, ";")
    For Each V In Acol
      If UBound(Filter(Bcol, V, True)) = -1 Then Cell.Characters(InStr(";" & Cell.Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
    For Each V In Bcol
      If UBound(Filter(Acol, V, True)) = -1 Then Cell.Offset(, 1).Characters(InStr(";" & Cell.Offset(, 1).Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
  Next
End Sub
Thanks for introduction to VBA filter function. First time seeing it and it is quite efficient in this type of application.
 
Upvote 0
Although I used the same setup as JGordon11 did, this macro uses a completely different (much more compact) approach than he used. And while I did not test for it, I see no reason it should have any size limitations. See if it works for you...
VBA Code:
Sub HighlightUniques()
  Dim Cell As Range, V As Variant, Acol As Variant, Bcol As Variant
  For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
    Cell.Font.Color = vbBlack
    Cell.Offset(, 1).Font.Color = vbBlack
    Acol = Split(Cell.Value, ";")
    Bcol = Split(Cell.Offset(, 1).Value, ";")
    For Each V In Acol
      If UBound(Filter(Bcol, V, True)) = -1 Then Cell.Characters(InStr(";" & Cell.Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
    For Each V In Bcol
      If UBound(Filter(Acol, V, True)) = -1 Then Cell.Offset(, 1).Characters(InStr(";" & Cell.Offset(, 1).Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
  Next
End Sub
This works great. My only question is that it appears that it will compare the values in Columns A and B. Could this be modified so that I may define the columns to be compared. The two columns I need to compare are dynamic and do not always appear in the same columns for different worksheets. The solution JGordon11 provided allowed me to select the range I need to compare.
 
Upvote 0
Try this version of my code then... it will ask you to type in the two column designations (letters or numbers) with a comma between them.

**** SEE MESSAGE #12 FOR CORRECTED CODE ****
Rich (BB code):
Sub HighlightUniques()
  Dim Col1 As String, Col2 As String, ColsIn As String
  Dim Cell As Range, V As Variant, Acol As Variant, Bcol As Variant
  ColsIn = InputBox("Type the letter or number designation for the two columns with a comma between them.")
  If (Not ColsIn Like "*?,?*") Or (ColsIn Like "*,*,*") Then Exit Sub
  Col1 = Left(ColsIn, InStr(ColsIn, ",") - 1)
  Col2 = Mid(ColsIn, InStr(ColsIn, ",") + 1)
  For Each Cell In Range(Cells(1, Col1), Cells(Rows.Count, Col1).End(xlUp))
    Cell.Font.Color = vbBlack
    Cells(Cell.Row, Col2).Font.Color = vbBlack
    Acol = Split(Cell.Value, ";")
    Bcol = Split(Cells(Cell.Row, Col2).Value, ";")
    For Each V In Acol
      If UBound(Filter(Bcol, V, True)) = -1 Then Cell.Characters(InStr(";" & Cell.Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
    For Each V In Bcol
      If UBound(Filter(Acol, V, True)) = -1 Then Cells(Cell.Row, Col2).Characters(InStr(";" & Cells(Cell.Row, Col2).Value & ";", ";" & V & ";"), Len(V)).Font.Color = vbRed
    Next
  Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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