VBA Match data in Multiple Columns return True if any matches

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good afternoon,

I have some sample data below I really have approx 26 columns. I would like a VBA solution to see if any value equals the other in a range of cells that I select. For the data set, it is only 2 columns but you get the idea. I just need True if there are any matches. Any help is appreciated.

20202021
1414TRUE
13, 1113TRUE
11, 1313, 17TRUE
1413
16, 18, 19, 226, 4, 14
56, 32, 111111, 22TRUE
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I think this mehtod could be a bit complicated for 26 columns but I couldn't think of a better way of doing this:
VBA Code:
Sub tst()
Dim tempd As Double
Dim cola As Object
Set cola = CreateObject("scripting.dictionary")
cola.CompareMode = vbTextCompare
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(LastRow, 2)) ' input data
outarr = Range(Cells(1, 3), Cells(LastRow, 3)) ' output data
For i = 2 To LastRow
  outarr(i, 1) = False
  If InStr(inarr(i, 1), ",") Then ' check if there is a comma
     tempa = Split(inarr(i, 1), ",")
    For j = 0 To UBound(tempa)
     tempd = tempa(j) ' change string to double
     cola(tempd) = 0
    Next j
  Else
    cola(inarr(i, 1)) = 0
  End If
  If InStr(inarr(i, 2), ",") Then ' check if the is a comma
    colb = Split(inarr(i, 2), ",")
    For j = 0 To UBound(colb)
        tempd = colb(j)
        If cola.exists(tempd) Then
         outarr(i, 1) = True
         Exit For
        End If
    Next j
  Else
    tempd = inarr(i, 2)
    If cola.exists(tempd) Then
     outarr(i, 1) = True
    End If
  End If
 cola.RemoveAll
Next i
Range(Cells(1, 3), Cells(LastRow, 3)) = outarr

End Sub
 
Upvote 0
offthelip that does it! Thank you very much for your help! Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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