简体   繁体   English

Excel VBA-匹配函数错误(无法获取匹配属性)

[英]Excel VBA - Match function error (Unable to get match property)

I'm trying to do a match search to filter out rows where certain data is matched. 我正在尝试进行匹配搜索以筛选出某些数据匹配的行。 For example, I have "ButtonName" which shows which column filter has to be put and " DataRange.Rows(1) " is the range on which it tries to search for the match. 例如,我有“ ButtonName”,它显示了必须放置哪个列过滤器,而“ DataRange.Rows(1) ”是它尝试搜索匹配项的范围。 Sometimes when I do some editing, it shows: 有时,当我进行一些编辑时,它显示:

object defined error 对象定义的错误

And now it is showing: 现在它显示:

Unable to get the match property 无法获得比赛属性

Please somebody tell me what is the error in the code? 请有人告诉我代码中的错误是什么?

Private Sub CommandButton21_Click()

    Dim myButton As OptionButton
    Dim SearchString As String
    Dim ButtonName As Variant
    Dim sht As Worksheet
    Dim myField As Long
    Dim DataRange As Range
    Dim mySearch1, mySearch2, mySearch3 As Variant

    'Load Sheet into A Variable
      Set sht = ActiveSheet
      Set a = ActiveSheet
    'Unfilter Data (if necessary)
      On Error Resume Next
      sht.ShowAllData
      On Error GoTo 0

    'Filtered Data Range (include column heading cells)
       Set DataRange = sht.Range("A13:AL3000") 'Cell Range

    'Retrieve User's Search Input

    mySearch1 = sht.Range("D4").Text 'Control Form     ''Contains data entered in D4cell

     ButtonName = sht.Range("M12").Text     

     If Not IsError(WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0))          Then

        myField = WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
     Else
       MsgBox "no match is found in range(" & rngToSearch.Address & ")."
     End If


    'Filter Data
       DataRange.AutoFilter _
       Field:=myField, _
       Criteria1:="=*" & mySearch1 & "*", _
       Operator:=xlAnd, _
       Criteria2:="=*" & mySearch2 & "*", _
       Operator:=xlAnd, _
       Criteria2:="=*" & mySearch3 & "*", _
       Operator:=xlAnd

End Sub

The Error occurs on this line: 该行发生错误:

If Not IsError(WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0))

Make the following changes: 进行以下更改:

Dim myField As Variant   'IMPORTANT

myField = Application.Match(ButtonName, DataRange.Rows(1), 0)

If IsError(myField) Then
    MsgBox "no match is found in range(" & rngToSearch.Address & ")."
Else
    'Filter Data
    DataRange.AutoFilter _
    Field:=myField, _
    Criteria1:="=*" & mySearch1 & "*", _
    Operator:=xlAnd, _
    Criteria2:="=*" & mySearch2 & "*", _
    Operator:=xlAnd, _
    Criteria2:="=*" & mySearch3 & "*", _
    Operator:=xlAnd
End If

尝试对表达式求值:

If Evaluate("ISERROR(MATCH(""" & ButtonName & """," & Selection.Rows(1).Address & ",0))") Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA错误处理:无法获取工作表函数类的match属性 - VBA Error Handling: Unable to get the match property of the worksheet function class Excel VBA:无法获取匹配项,错误“无法获取 WorksheetFunction 类的匹配属性” - Excel VBA: Can't get a match, error "Unable to get the Match property of the WorksheetFunction class" Excel VBA:“无法获取工作表函数类的match属性”,应存在匹配项时出错 - Excel VBA: “unable to get the match property of the worksheetfunction class” Error when match should exist 匹配不工作Excel:错误1004无法获取匹配属性 - Match Not working Excel: Error 1004 Unable to get the Match Property Excel 错误匹配“无法获取工作表函数类的匹配属性” - Excel error with match "Unable to get the Match property of the worksheetfunction class" Excel VBA无法获取WorksheetFunction类的Match属性 - Excel VBA unable to get the Match property of the WorksheetFunction class Excel VBA:无法获取工作表函数类的匹配属性运行时错误 1004 - Excel VBA: unable to get the match property of the worksheetfunction class Run-time error 1004 匹配函数错误Excel VBA - Match function error excel vba VBA:无法获取WorkSheet函数类的Match属性 - VBA: Unable to get the Match property of the WorkSheet function class VBA:无法获取WorkSheetFunction类的Match属性 - VBA : Unable to get the Match property of WorkSheetFunction class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM