当application.Match
函数无法找到匹配项时,会返回一个错误值。为了避免这种情况,你可以使用IsError
函数来检查application.Match
返回的值是否为错误值。以下是一个示例代码:
Dim result As Variant
Dim rng As Range
Set rng = Sheets("Sheet1").Range("A1:A10")
result = Application.Match("Not Found", rng, 0)
If IsError(result) Then
MsgBox "No match found."
Else
MsgBox "Match found at position " & result
End If
在这个示例中,我们使用Application.Match
函数在A1:A10
范围中寻找值为"Not Found"的项。如果找到了匹配项,result
会得到匹配项的位置。如果未找到匹配项,result
会得到一个错误值。然后,我们使用IsError
函数来检查result
是否是一个错误值。如果是错误值,显示"No match found."的消息框;否则,显示"Match found at position [位置]"的消息框。