Код ниже возвращает желаемый результат:
Sub TestMe()
Dim str As String: str = "apples*oranges"
Dim foundCell As Range
Dim options As Variant
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
Dim myVar As Variant
For Each myVar In options
With Worksheets(1)
Set foundCell = .Cells.Find(What:=myVar, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then Exit For
Next myVar
If Not foundCell Is Nothing Then
Debug.Print foundCell.Address
End If
End Sub
У нас есть две разные строки, для которых необходимо выполнить печать: "apples*oranges"
и "oranges*apples"
. Раскол и обратное сделано довольно примитивно:
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
И затем, используя For Each Loop
с ранним Exit For
, .Find()
ищет две строки.