查找部分string的行
以下是我正在使用的代码:
Dim Name As Long With Application.WorksheetFunction Name= .Match("Test:", Range("A1:A1000"), -1) End With
我正在寻找的是“Test:Hello”,但是Hello并不总是遵循testing,所以我不想使用完整匹配。 我看到了Instr
函数,但我想知道这个部分匹配的行。
您可以在公式中使用通配符*
来捕获Test:
后的任何内容。
我们想要返回确切的,所以第三个标准应该是0
:
Dim Name As Long With Application.WorksheetFunction Name= .Match("Test:*", Range("A1:A1000"), 0) End With
虽然这个方法vba会错误,如果没有find匹配。
我们可以删除WorksheetFunction部分,vba会传递错误,所以我们可以testing它:
Dim Name As Long With Application If IsError(.Match("Test:*", Range("A1:A1000"), 0)) then msgbox "No match found" Else Name= .Match("Test:*", Range("A1:A1000"), 0) End if End With