2017-02-15 90 views
0

我試圖根據兩個不同過濾器的兩個單元格值過濾數據透視表。我已經找到了代碼,我可以適應其中一個單元格,但我不確定如何去集成第二個單元格,該單元格位於下面的單元格中(請參閱下面我正在修改的代碼)使用2個單元格值過濾數據透視表

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

'This line stops the worksheet updating on every change, it only updates when cell 
'H6 or H7 is touched 
If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub 

'Set the Variables to be used 
Dim pt As PivotTable 
Dim Field As PivotField 
Dim NewCat As String 

'Here you amend to suit your data 
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1") 
Set Field = pt.PivotFields("Category") 
NewCat = Worksheets("Sheet1").Range("H6").Value 

'This updates and refreshes the PIVOT table 
With pt 
Field.ClearAllFilters 
Field.CurrentPage = NewCat 
pt.RefreshTable 
End With 

End Sub 

感謝您的幫助!

回答

0

經過進一步的思考,我想出了我的問題。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

'This line stops the worksheet updating on every change, it only updates when cell 
'H6 or H7 is touched 
If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub 

'Set the Variables to be used 
Dim pt As PivotTable 
Dim Field1 As PivotField 
Dim Field2 As PivotField 
Dim NewCat1 As String 
Dim NewCat2 As String 

'Here you amend to suit your data 
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1") 
Set Field1 = pt.PivotFields("Category1") 
Set Field1 = pt.PivotFields("Category2") 
NewCat1 = Worksheets("Sheet1").Range("H6").Value 
NewCat2 = Worksheets("Sheet1").Range("H7").Value 

'This updates and refreshes the PIVOT table 
With pt 
Field1.ClearAllFilters 
Field1.CurrentPage = NewCat1 
Field2.ClearAllFilters 
Field2.CurrentPage = NewCat2 
pt.RefreshTable 
End With 

End Sub 
相關問題