2017-02-15 96 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 
相关问题