【发布时间】:2016-12-19 08:04:11
【问题描述】:
我在工作表上插入了一个命令按钮,按下该按钮会打开密码用户表单。输入密码后,成本中心编号会进入工作表执行摘要。
下面的代码不会自动更新数据透视表,因为它需要更改目标单元格。
如何更改以下代码以在每次目标单元格更改时更新?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates
'when cell G1 is touched
If Not Intersect(Target, Range("G1")) 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("Executive Summary").PivotTables("ServiceCostAnalysis")
Set Field = pt.PivotFields("Cost Centre")
NewCat = Worksheets("Executive Summary").Range("G1").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
谢谢
【问题讨论】:
标签: vba excel pivot-table excel-2013