Run time error '1004' Unable to get the Match propertyof the WorksheetFunction class

前端 未结 6 1312
悲&欢浪女
悲&欢浪女 2021-01-17 01:29

In my macro, I have the following code :

i = Application.WorksheetFunction.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)
6条回答
  •  野的像风
    2021-01-17 02:12

    Try this instead:

    If Not IsError(Application.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)) Then
        i = Application.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)
    Else
        'do something if no match is found
    
    End If
    

    Update

    Here is better code that does not rely on Selection except as a means of user-input for defining the range to be searched.

    Sub Test()
    
    Dim str_accrual As String
    Dim rngToSearch As Range
    
    str_accrual = InputBox("Search for?")
    
    Set rngToSearch = Range(Selection, Selection.End(xlToRight))
    
    If Not IsError(Application.Match(str_accrual, rngToSearch, 0)) Then
        i = Application.Match(str_accrual, rngToSearch, 0)
        MsgBox i
    Else
        MsgBox "no match is found in range(" & rngToSearch.Address & ")."
    End If
    End Sub
    

提交回复
热议问题