My function is supposed to check a cell for a string, and then return that string if its present, and return an empty string if it's not. The first two parts are working fine--it checks, and it returns the string it it's present, but if the string isn't present, it returns an error (#value). What's happening?
Function Keyword(cell As Range, word As String)
'checks given cell for keyword, returns keyword if present
If (WorksheetFunction.IsNumber(WorksheetFunction.Search(word, cell.Range("A1"), 1))) Then
Keyword = word + ", "
Else: Keyword = ""
'returns empty string if not present
End If
End Function
Call it like this: =Keyword(H2,"LED")
If the value you are looking for isn't found, you'll get run-time errors, and your function will fail. Easier to use InStr
to search for the value:
If Instr(1, cell.Range("A1").Value, word, vbTextCompare) <> 0 Then Keyword = word & ", "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.