[英]Custom Excel VBA function returning error in some cases, not in other
我的函數應該檢查一個單元格是否有字符串,然后返回該字符串(如果存在),否則返回一個空字符串。 前兩個部分工作正常-檢查並返回存在的字符串,但是如果不存在,則返回錯誤(#value)。 發生了什么?
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
這樣命名:= Keyword(H2,“ LED”)
如果找不到所需的值,則會出現運行時錯誤,並且功能將失敗。 使用InStr
來搜索值更容易:
If Instr(1, cell.Range("A1").Value, word, vbTextCompare) <> 0 Then Keyword = word & ", "
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.