我尝试进行匹配搜索,以过滤掉某些数据匹配的行。例如,我有" ButtonName"它显示了必须放置哪个列过滤器和" DataRange.Rows(1)
"是它尝试搜索匹配的范围。有时当我进行一些编辑时,会显示:
对象定义错误
现在它显示:
无法获取匹配属性
请有人告诉我代码中的错误是什么?
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
此行发生错误:
If Not IsError(WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0))
答案 0 :(得分:0)
进行以下更改:
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
答案 1 :(得分:0)
尝试评估表达式:
If Evaluate("ISERROR(MATCH(""" & ButtonName & """," & Selection.Rows(1).Address & ",0))") Then