【发布时间】:2015-08-19 15:38:57
【问题描述】:
我正在尝试根据两个单元格值刷新我的数据透视表。最终用户将在单元格 B4(区域)中输入值,在单元格 B5(部门)中输入值。数据透视表将根据这些值刷新。
我在下面找到了代码,但只允许引用 1 个单元格值。不知道如何为 2 个单元格值修改它。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'B4 or B5 is touched
If Intersect(Target, Range("B4:B5")) 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 filter your data
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Region")
NewCat = Worksheets("Sheet1").Range("B4").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
【问题讨论】:
标签: excel pivot-table vba