I'm trying to define an Excel function in VBA where I can specify Apple or Orange as in =MyFunc("Apple")
and get returned "Tom" or "Dick, Harry". I'm able to figure out which Row the search query is on using Find
but I can't figure out how to scan part of that Row for "X" (or not empty) and return the value(s) from the top Row corresponding to the "X".
... B ... M N ... CR
___________________________________
3 | | Tom | Dick | Harry
+--------+-------+--------+--------
4 | Apple | X | |
+--------+-------+--------+--------
5 | Orange | | X | X
What I've got so far:
Function MyFunc(what As String, Optional sep As String = ", ") As String
Dim rngSearch As Range, rngFound As Range
Dim strResult As String, allNames As Range
Set rngSearch = Worksheets("Sheet1").Range("B:B")
Set allNames = Worksheets("Sheet1").Range("M3:CR3")
Set rngFound = rngSearch.Find(what, LookIn:=xlValues, LookAt:=xlPart)
If rngFound Is Nothing Then
MsgBox "Not found"
Else
MsgBox rngFound.Row
'search that row from Col M to Col CR for "X", add value in Row 3 to strResult if X is found
End If
MyFunc = strResult
End Function