2016-07-29 184 views
2

我有一張表,我需要返回所有切換的值。當我錄製選擇後一個宏我希望它看起來像這樣excel VBA - return Criteria1來自自動篩選器的數組

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

我的問題是,A,B,C,將由用戶來過濾等價值總是會變化的,所以我哪些不能以任何方式對任何標準進行硬編碼。

是有辦法,我可以返回什麼是類似於如何看起來時尚切換上的陣列?

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

回答

5

與像數據開始:

enter image description here

這個子應用過濾器:

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 

enter image description here

,這會子:

  • 確定哪些列過濾過濾列,其中有選擇做
  • 列表中的實際選擇


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