下面的行是我一起工作的代码:查找部分字符串
Dim Name As Long
With Application.WorksheetFunction
Name= .Match("Test:", Range("A1:A1000"), -1)
End With
全是我寻找的是“测试:你好”,但这个词您好并不总是遵循的测试,所以我不想使用完整匹配。我看到了Instr
函数,但我想知道这个部分匹配的行。
下面的行是我一起工作的代码:查找部分字符串
Dim Name As Long
With Application.WorksheetFunction
Name= .Match("Test:", Range("A1:A1000"), -1)
End With
全是我寻找的是“测试:你好”,但这个词您好并不总是遵循的测试,所以我不想使用完整匹配。我看到了Instr
函数,但我想知道这个部分匹配的行。
您可以在公式中使用通配符*
来捕获Test:
之后的任何内容。
而且我们要返回的确切所以第三个标准应该是0
:
Dim Name As Long
With Application.WorksheetFunction
Name= .Match("Test:*", Range("A1:A1000"), 0)
End With
但如果没有找到匹配这种方法VBA会报错了。与通配符`
我们可以删除WorksheetFunction部分和VBA将通过错误,所以我们可以测试它:
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
@AdamWarner请参阅编辑更多信息。 –
任何想法是否'.Match'比'Find()。row'更好用吗? – BruceWayne
@BruceWayne我曾经辩论过一段时间,有人提供了一个链接到一个页面,其他人已经完成了测试,Match比查找更快。这是[链接](https://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/) –
尝试'“*测试”。 –
我认为'-1'需要为'0'才能找到正确的行。 –
我不知道你可以在非Ranges中使用'With'。整齐地知道。你也可以使用'Find()':'Range(“A:A”)。Find(What:=“Test:”,LookAt:= xlPart).Row',但@ScottCraner有你的答案和通配符。 – BruceWayne