Compare two columns of data, not exact order

kswny0775

New Member
Joined
Apr 27, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I am looking to compare two columns of data to see if they contain the same data regardless of order.

Example:

A1: Apple; Orange; Cherry
B2: Apple; Cherry; Orange
Match -- both contain all the same values, but not in the same order

A2: Cherry; Orange
B2: Cherry; Apple
No Match -- both do not contain ALL the same values
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You could do it with a UDF

Book1
ABC
1Apple; Orange; CherryApple; Cherry; OrangeMatch
2Cherry; OrangeCherry; AppleNo Match
3Cherry; Orange Orange; CherryMatch
4CherryCherry; Cherry; CherryNo Match
5CherryCherryMatch
6Apple; Orange; CherryApple; Cherri; OrangeNo Match
Sheet8
Cell Formulas
RangeFormula
C1:C6C1=compare(A1,B1)


VBA Code:
Function Compare(S1 As String, S2 As String) As String
    Dim IsEqual As Boolean
    Dim SD As Object
    Dim SA1 As Variant, SA2 As Variant, X As Variant

    Set SD = CreateObject("Scripting.dictionary")
    SA1 = Split(Replace(S1, " ", ""), ";")
    SA2 = Split(Replace(S2, " ", ""), ";")

    If UBound(SA1) <> UBound(SA2) Then
        IsEqual = False
    Else
        IsEqual = True
        For Each X In SA1
            If Not SD.exists(X) Then
                SD.Add X, ""
            End If
        Next X
        For Each X In SA2
            If Not SD.exists(X) Then
                IsEqual = False
                Exit For
            End If
        Next X
    End If

    If IsEqual Then
        Compare = "Match"
    Else
        Compare = "No Match"
    End If
    Set SD = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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