excel VBA - return Criteria1 Array from an Autofilter

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I have a table that I need to return all of the toggled values. When i record a macro after selecting which ones i want it looks like this

ActiveSheet.Range("$A$1:$P$1000").AutoFilter Field:=6, Criteria1:=Array("A" _     , "B", "C", "D", "E", "G"), Operator:=xlFilterValues 

the problem i have is that the a,b,c, etc values that will be filtered by the user will always be changing so I can't hardcode any criteria that way.

is there a way i can return an array of what is toggled on in a fashion similar to how this looks?

msgbox ActiveSheet.Range("$A$1:$P$1000").criteria1 

回答1:

Beginning with data like:

this sub applies a filter:

Sub Macro1()     Range("A1:C22").Select     Selection.AutoFilter     ActiveSheet.Range("$A$1:$C$22").AutoFilter Field:=3, Criteria1:=Array( _         "Alice", "Boris", "Mike"), Operator:=xlFilterValues End Sub 

and this sub will:

  • identify which columns are filtered
  • of the filtered columns, which have selections made
  • list the actual selections


Sub FilterInformation()     Dim st As String, ws As Worksheet, rg As Range, boo As Boolean      Set ws = ActiveSheet     On Error GoTo GetMeOut     Set rg = ws.AutoFilter.Range      MsgBox "Filter range" & vbCrLf & rg.Address      N = ws.AutoFilter.Filters.Count     MsgBox "Number of filters" & vbCrLf & N      For i = 1 To N         boo = ws.AutoFilter.Filters.Item(i).On         MsgBox i & "==>" & boo         If boo Then             MsgBox UBound(ws.AutoFilter.Filters.Item(i).Criteria1) & " items in array"             U = UBound(ws.AutoFilter.Filters.Item(i).Criteria1)             L = LBound(ws.AutoFilter.Filters.Item(i).Criteria1)             For j = L To U                 MsgBox ws.AutoFilter.Filters.Item(i).Criteria1(j)             Next         End If      Next     Exit Sub GetMeOut:     MsgBox ("no filters in sheet") End Sub 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!