Вот код с некоторыми пояснениями в комментарии (пример данных ниже):
Sub StringsAttached()
' looks in each cell in sheet "Base" col A for any names in sheet "Broking"
' if found, writes 'Y' in sheet "Base" col B, 'N' if not found
Dim TotalRows As Long ' last row in sheet "Base"
Dim StrToFind As Variant ' will be an array
Dim i As Integer ' to iterate on each row of sheet "Base"
Dim strToCheck As String ' string to check
Dim item As Variant ' string from sheet "Broking"
Dim ispresent As Integer ' result of InStr (search in string)
TotalRows = Sheets("Base").Range("A" & Rows.Count).End(xlUp).Row
StrToFind = Worksheets("Broking").Range("A2:A6") ' range with brokers names
For i = 2 To TotalRows ' Loop 1: iterates each row, omitting heading row (1)
strToCheck = Sheets("Base").Range("A" & i)
For Each item In StrToFind ' Loop 2: look for each broker names in turn
ispresent = InStr(strToCheck, item) ' search in string
If ispresent <> 0 Then
Sheets("Base").Range("B" & i) = "Y"
GoTo exitWhenFound ' when one name is found, exit Loop 2
Else
Sheets("Base").Range("B" & i) = "N"
End If
Next item
exitWhenFound:
Next
End Sub
У меня есть два листа в рабочей книге: «База» и «Брокинг».
В «Base» таблица выглядит так:
Strings to check Yes or No
John, Paul, George, Ringo Y
Harry, Hermione, Ron N
Doug, Vanessa, Jim, Claire Y
Vanessa, Claire N
Ornella, Renzo, Alberto N
Renzo, Ornella N
Tim, Bruno, Doug Y
Mary, Renzo Y
Jean, Pierre, Donald N
Jim, Jean, Bruno Y
Renzo, Mike Johnson Y
Ornella, Mike Smith N
А в листе «Broking»:
Names
Doug
Jim
Mary
John
Mike Johnson