2016-09-05 37 views
1

所以我對excel和VBA非常不熟悉,但在過去的幾周裏試圖深入研究它。我需要爲包含相同數據(和列)的多個工作表設置一個過濾器。用於過濾在多張相同的範圍內我已經發現了一個簡單的解決方案:
excel vba:爲sheet2創建過濾器,並將數據從應用於sheet1的過濾器中取出

Sub apply_autofilter_across_worksheets() 

    Dim p As Integer, q As Integer 

    p = Worksheets.Count 

    For q = 1 To p 
    With Worksheets(q) 
    .AutoFilterMode = False 
    .Range("A1").AutoFilter 
    .Range("A1").AutoFilter Field:=1, Criteria1:="2" 
    End With 
    Next q 

End Sub 

真正的麻煩從這裏開始:我想在片材1的過濾器設定爲第2列,從第1列取所得到的數據並將其用作表2(最終3)的過濾標準。牀單是這個樣子:

工作表Sheet1

| itemgroup | subject | course 
|   1 | biology | B.Sc. 
|   1 | chemistry| B.Sc. 
|   1 | history | M.Sc. 
|   2 | biology | B.Sc. 
|   2 | history | B.Sc. 
|   3 | chemistry| B.Sc. 

Sheet2中

| itemgroup | items 
|   1 | Example 
|   1 | Example 
|   2 | Example 
|   3 | Example 

比如我想在Sheet1設置爲過濾器 「生物學」,然後選擇 「1」 和「 2「應設置爲表2中第1列的過濾器。
我已使用下面的代碼。過濾器被設置爲兩個工作表,但工作表2僅被一個標準過濾。結果是這樣的:

Sheet2 after Filter applied

這是數據我應用過濾器:https://drive.google.com/open?id=0B6wLL0wGBKsNWHJ3bDYtdVd0cEE

我使用的代碼:

Option Explicit 
Sub main() 
    Dim cell As Range, filtValuesRng As Range 

    With Worksheets("Itemgruppen") '<--| reference worksheet "Sheet1" 
     With .Range("A1").CurrentRegion '<-- reference its data set 
      .AutoFilter 2, "Biologie" '<--| filter it on column 2 with  criteria="biology" 
      If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then Set filtValuesRng = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible) '<--| if any value filtered then set them into a range 
     End With 
     '.AutoFilterMode = False '<--| show all rows back and remove filters 
    End With 

    If filtValuesRng Is Nothing Then Exit Sub '<--| if no values filtered from previous "Sheet1" column 2 filtering then exit 

    With Worksheets("Itembloecke") '<--| reference worksheet "Sheet2" 
     With .Range("A1").CurrentRegion '<-- reference its data set 
      For Each cell In filtValuesRng '<--| iterate over "Sheet1" column filtered values 
       .AutoFilter 1, cell.Value2 '<--| filter worksheet "Sheet2" dataset on column 1 with current "Sheet1" column filtered value 
       If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered... 
'     .SpecialCells(xlCellTypeVisible)... '<--| do something with filtered cells 
       End If 
      Next 
     End With 
     '.AutoFilterMode = False '<--| show all rows back and remove filters 
    End With 
End Sub 


這是我的第一個問題堆棧溢出 - 如果您對如何更好地提出問題有任何建議,我會很感激。

回答

0

這是否解決您的問題?

Sub apply_autofilter_across_worksheets() 

    Dim sht As Worksheet 

    For Each sht In ThisWorkbook.Worksheets ' loop over all sheets 
     sht.AutoFilterMode = False ' remove current filter 

     sht.Range("A1").AutoFilter ' add new filter 
     If sht.Name = "Sheet1" Then ' specifics for Sheet1 
      sht.Range("A1").AutoFilter _ ' specify the options for the filter 
       Field:=2, _ ' add criteria to second column 
        Criteria1:="biology" 
     End If 
     If sht.Name = "Sheet2" Then ' specifics for Sheet2 
      sht.Range("A1").AutoFilter _ ' specify the options for the filter 
       Field:=1, _ ' add criteria to first column 
        Criteria1:=Array("1", "2"), Operator:=xlFilterValues ' add multiple criterias 
     End If 

    Next sht 


End Sub 
+0

它設置在Sheet1中的過濾器,但設置在Sheet2的一個完全不同的過濾器。 – Robn

+0

@Robn這是我以爲你想要的? _例如,我想在sheet1中設置爲過濾器「biology」,然後在表2中爲第1列設置「1」和「2」作爲過濾器._ –

+0

@Robn在代碼中添加了註釋...此外,我只是理解了您的問題現在_真正的麻煩從這裏開始:我想在表1到第2列中設置一個過濾器,從第1列獲取結果數據並將其用作表2的過濾標準(最終3)._ –

0

你可以試試這個(註釋)代碼:

Option Explicit 

Sub main() 
    Dim cell As Range, filtValuesRng As Range 

    With Worksheets("Sheet1") '<--| reference worksheet "Sheet1" 
     With .Range("A1").CurrentRegion '<-- reference its data set 
      .AutoFilter 2, "biology" '<--| filter it on column 2 with criteria="biology" 
      If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then Set filtValuesRng = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible) '<--| if any value filtered then set them into a range 
     End With 
     .AutoFilterMode = False '<--| show all rows back and remove filters 
    End With 

    If filtValuesRng Is Nothing Then Exit Sub '<--| if no values filtered from previous "Sheet1" column 2 filtering then exit 

    With Worksheets("Sheet2") '<--| reference worksheet "Sheet2" 
     With .Range("A1").CurrentRegion '<-- reference its data set 
      For Each cell In filtValuesRng '<--| iterate over "Sheet1" column filtered values 
       .AutoFilter 1, cell.Value2 '<--| filter worksheet "Sheet2" dataset on column 1 with current "Sheet1" column filtered value 
       If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered... 
'     .SpecialCells(xlCellTypeVisible)... '<--| do something with filtered cells 
       End If 
      Next 
     End With 
     .AutoFilterMode = False '<--| show all rows back and remove filters 
    End With 
End Sub 
+0

感謝評論代碼!有一個問題:只有「itemgroup」(過濾後)中的最後一個條目用作sheet2中的過濾器 - 以我的數據結構爲例:在將「biology」設置爲「biology」後,只有「2」被設置爲sheet2中的Filter過濾到工作表1. – Robn

+0

做「工作表1」列有標題?根據你的例子,我假設他們有 – user3598756

+0

我'插入'一個表,第一行包含列的標題(在Sheet1和2) - 這是你的意思? – Robn