this code uses regex to match the names. so you have to enable regex under extras --> references and select "Microsoft VBScript Regular Expressions 5.5". (read more about RegEx)
Sub MatchNames(col As Range, ws as Worksheet)
Dim regEx As New RegExp
Dim lRow As Long, lCol As Long
lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
Dim exp As String: exp = "(" & Join(Application.Transpose(ws.Range(ws.Cells(2, col.Column), ws.Cells(lRow, 1))), "|") & ")"
exp = Replace(exp, " ", "")
With regEx
.Global = True
.MultiLine = False
.IgnoreCase = True
.Pattern = exp
End With
Dim matched As String
For i = 1 To lCol
lRow = ws.Cells(ws.Rows.Count, i).End(xlUp).Row
Dim data As String: data = Join(Application.Transpose(ws.Range(ws.Cells(2, i), ws.Cells(lRow, i))), ";")
data = Replace(data, " ", "")
If regEx.Test(data) Then
matched = matched & ws.Cells(1, i).Value & ";"
End If
Next i
MsgBox ("Matched Columns:" & vbNewLine & Replace(matched, ";", vbNewLine))
End Sub
The parameters are any cell of the column with the names to match and the worksheet the data is in, i.e Call MatchNames(Worksheets(1).Cells(1,1), Worksheets(1))
to match Col A
on worksheet 1. it will then return a message box with all columns that had a match. What it basically does is to create a pattern with all names of that column and this will be matched with a joined string of all cells of column to match. As it only takes n steps for n columns the runtime should be pretty good aswell for all sizes of data.