I want to have my own function, that is able to search the same area on several worksheets. My idea was to go through a set of sheets (here all sheets in the workbook) and use Find to search that area. Now if the search was successful on the first sheet, I don’t want the result to be overwritten from the unsuccessful searches on the following, there for the if condition.
Function SheetsFind(LookUpValue As Integer) As Variant
Dim SearchRange As Range
For Each WS In Sheets
Set SearchRange = WS.Range("A1:B6")
If (SearchRange.Find(LookUpValue, LookIn:=xlValues, LookAt:=xlWhole) <> "Nothing") Then
SheetsFind = SearchRange.Find(LookUpValue, LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1).Value
End If
Next WS
End Function
The problem now is, if the find in the condition is unsuccessful, the function is left and I get a #value error.
Why does my function not just continue with the the next iteration?
Range.Find returns another range object, for this reason you’re getting problems.
Try this: