How to ***** if a list of comma separated text values is a subset of another list of comma separated text values

jahmaira

New Member
Joined
Jun 18, 2016
Messages
4
Hello!

This is my first question, I hope someone can help me and I hope I will be clear in explaining myself.

So I have two columns , A and B, in each row I have a list of text values separated by ",". What I would like to know is whether all the elements present in B are also present in A.

It will be brilliant to have something similar to: IF(ISNUMBER(SEARCH(B1;A1));1;0) but instead of having a single value to look for in B1 I have a list. I cannot use {("item1","item2","item3")} because the list of elements in both A and B change for every row.

here an example of my data set:


ABC
AP,HU,SU,WIAP,HU1
BE,ZD,ZO,AP,HU
BE,ZO,ZD1
IR,OL,WI,MA,CAIR,OL,NI,GW0
EM,SU,AP,HUEM,SU,MA,CA,AP,HU0

<tbody>
</tbody>

I'm using excel for mac 2011

Thank you very much in advance to anyone who will take time to help me :D
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
VBA might be the simplest solution.
Code:
Sub compareLists()
Dim i As Long, j As Long, c As Object, colA As Variant, colB As Variant, neg As Long
    With ActiveSheet
        For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            colB = Split(.Cells(i, 2), ",")
            For j = LBound(colB) To UBound(colB)
                    If InStr(.Cells(i, 1), colB(j)) = 0 Then
                        neg = 1
                        Exit For
                    End If
            Next
                If neg = 1 Then
                    .Cells(i, 3) = 0
                Else
                    .Cells(i, 3) = 1
                    neg = 0
                End If
        Next
    End With
End Sub
 
Upvote 0
Hi and welcome to the MrExcel Message Board.

I see that JLGWhiz has beaten me to it but I have a slightly different macro solution:
Code:
Function myMatch(r1 As Range, r2 As Range) As Long
    Dim arr1  As Variant
    Dim arr2  As Variant
    Dim ele1  As Variant
    Dim ele2  As Variant
    Dim mCnt  As Long
    
    arr1 = Split(r1, ",")
    arr2 = Split(r2, ",")
    For Each ele1 In arr2
        For Each ele2 In arr1
            If Trim(ele1) = Trim(ele2) Then
                mCnt = mCnt + 1
                Exit For
            End If
        Next
    Next
    If mCnt = UBound(arr2) - LBound(arr2) + 1 Then myMatch = 1 Else myMatch = 0
End Function
I have created a User-Defined Function called myMatch. You paste the above code into a new Module then you can use the new function in the worksheet:

Excel 2013
ABC
1ABC
2AP,HU,SU,WIAP,HU1
3BE,ZD,ZO,AP,HUBE,ZO,ZD1
4IR,OL,WI,MA,CAIR,OL,NI,GW0
5EM,SU,AP,HUEM,SU,MA,CA,AP,HU0
Sheet3
Cell Formulas
RangeFormula
C2=myMatch(A2,B2)
C3=myMatch(A3,B3)
C4=myMatch(A4,B4)
C5=myMatch(A5,B5)


Regards,
 
Upvote 0
Had a coding error in the first one.
Code:
Sub compareLists()
Dim i As Long, j As Long, c As Object, colA As Variant, colB As Variant, neg As Long
    With ActiveSheet
        For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            colB = Split(.Cells(i, 2), ",")
            For j = LBound(colB) To UBound(colB)
                    If InStr(.Cells(i, 1), colB(j)) = 0 Then
                        neg = 1
                        Exit For
                    End If
            Next
                If neg = 1 Then
                    .Cells(i, 3) = 0
                    neg = 0
                Else
                    .Cells(i, 3) = 1
                End If
        Next
    End With
End Sub
 
Upvote 0
Hi and welcome to the MrExcel Message Board.

I see that JLGWhiz has beaten me to it but I have a slightly different macro solution:
Code:
Function myMatch(r1 As Range, r2 As Range) As Long
    Dim arr1  As Variant
    Dim arr2  As Variant
    Dim ele1  As Variant
    Dim ele2  As Variant
    Dim mCnt  As Long
    
    arr1 = Split(r1, ",")
    arr2 = Split(r2, ",")
    For Each ele1 In arr2
        For Each ele2 In arr1
            If Trim(ele1) = Trim(ele2) Then
                mCnt = mCnt + 1
                Exit For
            End If
        Next
    Next
    If mCnt = UBound(arr2) - LBound(arr2) + 1 Then myMatch = 1 Else myMatch = 0
End Function
I have created a User-Defined Function called myMatch. You paste the above code into a new Module then you can use the new function in the worksheet
Here is another (slightly more compact) way to write your UDF...
Code:
Function Matches(Text As String, Source As String) As Long
  Dim Cnt As Long, V As Variant, Arr() As String
  For Each V In Split(Source, ",")
    Cnt = Cnt - (InStr(" " & Replace(Text, ",", " , ") & " ", " " & Trim(V) & " ") > 0)
  Next
  Matches = -(Cnt = Len(Source) - Len(Replace(Source, ",", "")) + 1)
End Function
Besides my renaming the function Matches, the only other difference between our two functions is that my function returns 0 when the second argument (Column B value) is blank (which I prefer) whereas your function returns 1.
 
Last edited:
Upvote 0
Here is another (slightly more compact) way to write your UDF...

And here's an even more compact way:
Code:
Function Matches(Text As String, Source As String) As Long
    Dim Cnt As Long, V As Variant
    For Each V In Split(Source, ",")
        Cnt = Cnt - (InStr(" " & Replace(Text, ",", " , ") & " ", " " & Trim(V) & " ") > 0)
    Next
    Matches = -(Cnt = Len(Source) - Len(Replace(Source, ",", "")) + 1)
End Function
No need to Dim Arr(). :)
Note:This code is not reliable if the input strings contain blanks.
 
Upvote 0
And here's an even more compact way:
Code:
Function Matches(Text As String, Source As String) As Long
    Dim Cnt As Long, V As Variant
    For Each V In Split(Source, ",")
        Cnt = Cnt - (InStr(" " & Replace(Text, ",", " , ") & " ", " " & Trim(V) & " ") > 0)
    Next
    Matches = -(Cnt = Len(Source) - Len(Replace(Source, ",", "")) + 1)
End Function
No need to Dim Arr(). :)
Heh-heh, yeah, I forgot to delete it from one of my earlier iterations of the UDF.


Note:This code is not reliable if the input strings contain blanks.
I am not so sure of that... can you post an example of two text strings where this is the case?
 
Upvote 0
I am not so sure of that... can you post an example of two text strings where this is the case?

For you, Rick, of course. ;)


Excel 2013
ABCD
1ABC
2AP,HU,SU,WIAP,HU,SU11
3AA BE,ZD,ZO,AP,HUZO,ZD,AA01
4IR,OL,WI,MA,CAIR,OL,NI,GW00
5EM,SU,AP,HUEM,SU,MA,CA,AP,HU00
Sheet3
Cell Formulas
RangeFormula
C2=myMatch(A2,B2)
C3=myMatch(A3,B3)
C4=myMatch(A4,B4)
C5=myMatch(A5,B5)
D2=matches(A2,B2)
D3=matches(A3,B3)
D4=matches(A4,B4)
D5=matches(A5,B5)


Regards,
 
Upvote 0
For you, Rick, of course. ;)

Excel 2013
ABCD
1A B C
2AP,HU,SU,WI AP,HU,SU11
3AA BE,ZD,ZO,AP,HU ZO,ZD,AA01
4IR,OL,WI,MA,CA IR,OL,NI,GW 00
5EM,SU,AP,HU EM,SU,MA,CA,AP,HU 00

<colgroup><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3
Oh, that kind of space (embedded where part of the text next to it matches one of the text being searched for).:oops: The solution for that problem (in my code) would require the user to "mean it" when they place a space next to a comma... that is, such a space cannot be used to "neaten up" the display... a space next to a comma would automatically be part of the text being searched for.
Code:
[table="width: 500"]
[tr]
	[td]Function Matches(Text As String, Source As String) As Long
    Dim Cnt As Long, V As Variant
    For Each V In Split(Source, ",")
        Cnt = Cnt - (InStr(Chr(1) & Replace(Text, ",", Chr(1) & "," & Chr(1)) & Chr(1), Chr(1) & V & Chr(1)) > 0)
    Next
    Matches = -(Cnt = Len(Source) - Len(Replace(Source, ",", "")) + 1)
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Rick, This is a test, isn't it?

You have omitted the Trim() function this time so strings in column B with trailing blanks are not located.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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