"运行时错误'':对象必需"

时间:2015-10-19 12:27:20

标签: excel vba object required

我想要一个函数来获取两个参数,一个搜索单元格和一个要搜索的单元格,其中搜索单元格包含逗号分隔列表。我希望对每个元素进行测试,以确定它是否在'中搜索'细胞。例如:

List_of_String: word1, word2, word3  
Where_to_Search: bbbbbword1 'Return TRUE'  
Where_to_Search: bbbbhhhhhmmmmm 'Return FALSE

我写道:

Function Search_in_String(List_of_Strings_to_Search As String, Where_to_Search As String)

String_array = Split(List_of_Strings_to_Search, ",")
Dim is_in_num As Integer
Dim is_in As Boolean

is_in = False

For i = 0 To (UBound(String_array) - 1)
    is_in_num = InStr(Where_to_Search, Trim(String_array(i)))
    If is_in_num > 0 Then is_in = True
    If is_in Then Exit For
Next i

Search_in_String = is_in
End Function



Sub test()
    MsgBox Search_in_String(BJ7.Value, Bk7.Value)
End Sub

test()函数我用来测试我的Search_in_String函数(我相信它也不起作用)。当我介入(F8)时,我收到错误:

  

运行时错误' 424':   需要对象

1 个答案:

答案 0 :(得分:2)

更改子测试():

Sub test()
    MsgBox Search_in_String(Range("BJ7").Value, Range("BJ7").Value)
End Sub