我正在尝试使用this answer解决MATCH
函数的字符限制:
= MATCH(TRUE,INDEX(ISNUMBER(SEARCH(A2,Dict!A:A)),0),0))
我尝试了完整版:
Sub test
Dim KEY_CONTRACT_ROW As Variant
KEY_CONTRACT_ROW = _
Application.WorksheetFunction.Match(True, _
Application.WorksheetFunction.Index(Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search("testing string", _
ThisWorkbook.Worksheets("Sheet1").Range("A:A"))), 0), 0)
End Sub
它不起作用-Type mismatch error
。
所以我尝试了较短的版本:
Sub test
MsgBox (Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search("testing string", _
ThisWorkbook.Worksheets("Sheet1").Range("A:A"))))
End Sub
达到相同的结果。同时,当我在工作表上手动使用此公式时,效果很好。
答案 0 :(得分:1)
WorksheetFunction
会在发生错误时产生中断错误。而是使用Application对象的Match方法。实际上,您可以完全避免使用WorksheetFunction
。
Sub test()
Dim KEY_CONTRACT_ROW As Variant
Dim rngSearchRange As Range
'Define the search range as to avoid a whole column reference
With ThisWorkbook.Worksheets("Sheet1")
Set rngSearchRange = Application.Intersect(.UsedRange, .Range("A:A"))
End With
'Search for the specified search string
With Application
KEY_CONTRACT_ROW = .Match(True, .IsNumber(.Search("testing string", rngSearchRange)), 0)
End With
'Check whether a match was found
If IsError(KEY_CONTRACT_ROW) Then
MsgBox "Match not found!", vbExclamation
Else
MsgBox "Match found!", vbExclamation
End If
End Sub