I'm not quite clear on what you're trying to achieve. Here is a copy of one of my primary search functions, it may be helpful to you.
Use like this to return the string name of the range of all cells that loosely contain the string "joe dirt". Note, with explicit false it turn off case sensitivity and search within strings. It would match "Joe Dirty 11/16/2017" The return might look like the string "A2:Z20"
Dim sJoeDirts as String
sJoeDirts = findCell("joe dirt", 0, , , False)
Use like this to return the first cell address that strictly contains the word "joe dirt". It will not match "Joe dirt". If "joe dirt" is not found, it will return the string "NotFound"
Dim sJoeDirt as String
sJoeDirt = findCell("joe dirt", 1, , , True)
Public Function findCell(searchString As String, Instance As Long, Optional xOffset As Integer, Optional yOffset As Integer, Optional Explicit As Boolean) As String
' Returns the cell address of the Nth instance of a word.
Dim searchMatches()
Dim searchMatch
Dim firstMatch
Dim Index As Long
Dim upBound As Long
Dim findAll As Boolean
Dim findAllRange As Range
Dim SD
Dim LA
Dim MC As Boolean
SD = xlNext
findAll = False
If Instance < 0 Then
SD = xlPrevious
Instance = Instance * -1
ElseIf Instance = 0 Then ' If instances are 0, we want all matches.
findAll = True
Instance = Application.WorksheetFunction.CountIf(myWorksheet.Cells, searchString)
End If
If Explicit Then
LA = xlWhole
MC = True
Else
LA = xlPart
MC = False
End If
Index = 0
' Search A1 first...
Set searchMatch = myWorksheet.Range("A1").Find(what:=searchString, LookIn:=xlFormulas, _
LookAt:=LA, SearchOrder:=xlByRows, SearchDirection:=SD, _
MatchCase:=MC, SearchFormat:=False)
' If we didn't find our first match in A1, then search on!
If searchMatch Is Nothing Then
Set searchMatch = myWorksheet.Cells.Find(what:=searchString, After:=myWorksheet.Range("A1"), LookIn:=xlFormulas, _
LookAt:=LA, SearchOrder:=xlByRows, SearchDirection:=SD, _
MatchCase:=MC, SearchFormat:=False)
End If
Set firstMatch = searchMatch ' Store first match
If findAll Then Set findAllRange = searchMatch ' Store in set if wanting all matches
Do Until Index = Instance ' Loop until we've found requested instance
If Not searchMatch Is Nothing Then 'If we found something
upBound = upBound + 1
ReDim Preserve searchMatches(upBound) ' Make room in array for it
searchMatches(Index) = searchMatch.offset(yOffset, xOffset).Address(False, False) ' Store address in array
If Instance > Index + 1 Then
Set searchMatch = myWorksheet.Cells.Find(what:=searchString, After:=searchMatch, LookIn:=xlFormulas, _
LookAt:=LA, SearchOrder:=xlByRows, SearchDirection:=SD, _
MatchCase:=MC, SearchFormat:=False) ' Search again if more are requested
If searchMatch.Address = firstMatch.Address Then ' If we loop back to first address
findCell = "TooMany"
If findAll Then findCell = findAllRange.Address
Exit Function
End If
If findAll Then Set findAllRange = Union(findAllRange, searchMatch)
End If
End If
Index = Index + 1
Loop
If firstMatch Is Nothing Then
findCell = "NotFound"
Exit Function
End If
If findAll Then
findCell = findAllRange.Address
Else
findCell = searchMatches(Instance - 1)
End If
End Function
Bookmarks