使用 vba 搜索功能时出现运行时错误 1004

run time error 1004 when I use vba search function

我正在练习一些功能

当我在它下面运行时returns运行时错误 1004 应用程序定义或对象定义的错误。

Public Sub se()
        a = 2
        While a < 6
            c = WorksheetFunction.IfError(WorksheetFunction.Search("1", "ella"), 100)
            MsgBox c
            a = a + 1
        Wend
End Sub

对于您似乎想要实现的目标,我建议改用 InStr 函数。 InStr 将搜索字符串并将 return 它首次出现的位置。如果搜索的字符串没有出现在正在搜索的字符串中,0 将被 returned。

使用方法如下:

InStr( [start], string, substring, [compare] )

一个工作示例 c/o Tech on the Net:

InStr(1, "Tech on the Net", "the")
'Result: 9'

InStr("Tech on the Net", "the")
'Result: 9'

InStr(10, "Tech on the Net", "t")
'Result: 15'

InStr("Tech on the Net", "z")
'Result: 0'