所以我将所有变量存储在一个字符串中,然后尝试将其传递给自动过滤条件,但无济于事。
Sub TestAutoFilter()
'Filter based on column Importance.
'PURPOSE: Dynamically Create Array Variable based on a Given Size
Dim myArray() As Variant
Dim DataRange As Range
Dim cell As Range
Dim x As Long
'Determine the data you want stored
Set DataRange = ActiveSheet.UsedRange
'Resize Array prior to loading data
'Loop through each cell in Range and store value in Array
counter = 0
For Each chkbx In ThisWorkbook.Sheets("Sheet1").CheckBoxes
If chkbx.Value > 0 And InStr(1, LCase(chkbx.Name), "check") <> 0 Then
counter = counter + 1
End If
Next chkbx
ReDim myArray(counter)
For Each chkbox In ThisWorkbook.Sheets("Sheet1").CheckBoxes
If chkbox.Value > 0 And InStr(1, LCase(chkbox.Name), "check") <> 0 Then
myArray(x) = chkbox.Caption
x = x + 1
End If
Next chkbox
myArrayString = ""
'Print values to Immediate Window (Ctrl + G to view)
For x = LBound(myArray) To counter
myArrayString = myArrayString & "," & myArray(x)
Next x
'Debug.Print (myArrayString)
myRightString = Left(myArrayString, Len(myArrayString) - 1)
myLeftString = Right(myRightString, Len(myRightString) - 1)
'Debug.Print (RTrim(myLeftString))
ThisWorkbook.Sheets("Data").Range("A3:F3").AutoFilter Field:=3, _
Criteria1:=Array(myLeftString), _
Operator:=xlFilterValues
End Sub
我也尝试将 myLeftstring 替换为实际数组,例如 Criteria1:=myArray
但这也不起作用。