我正在尝试创建一个自动过滤器,其中标准是由在工作表上选择的单元格组成的数组。
我在考虑像
这样的东西dim crit as variant
set crit = selection ' i know this isnt right
ActiveSheet.Range("$A$2:$AC$476").AutoFilter Field:=1, Criteria1:=Array(crit), Operator:=xlFilterValues
我不知道如何构建所选单元格数组作为标准传递。任何提示赞赏。
答案 0 :(得分:1)
你需要这样的东西:
Dim crit() As Variant
Dim n As Long
Dim rgCell As Range
ReDim crit(1 To Selection.Count)
n = 1
For Each rgCell In Selection.Cells
crit(n) = CStr(rgCell.Value)
n = n + 1
Next rgCell
ActiveSheet.Range("$A$2:$AC$476").AutoFilter Field:=1, Criteria1:=crit, Operator:=xlFilterValues
答案 1 :(得分:1)
这是你在尝试的吗?
Sub Sample()
Dim crit As Range
Dim Ar() As Variant
'~~> Check if what the user selected is a valid range
If TypeName(Selection) <> "Range" Then
MsgBox "Select a range first."
Exit Sub
End If
Set crit = Selection
Ar = crit.Value
ActiveSheet.Range("$A$1:$C$7").AutoFilter Field:=1, _
Criteria1:=Application.Transpose(Ar), _
Operator:=xlFilterValues
End Sub