【问题标题】:Loop through list and copy cell to the right循环遍历列表并将单元格复制到右侧
【发布时间】: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


    【解决方案1】:

    只需使用 Worksheet_Change 而不是 Worksheet_SelectionChange:

    Private Sub Worksheet_Change(ByVal Target As Range)
    

    每次更改工作表中的单元格时都会执行此过程。

    【讨论】:

    • 不幸的是,这不起作用。不知道为什么这很烦人
    • 请删除第二个代码行中的 Not 并重试。
    • 还是什么都没有。它没有调试或其他任何东西,只是现在没有更新数据透视表。
    • 所以我没记错:您希望您的数据透视表在单元格“G1”更改时更新?
    • 请在您的宏中设置几个暂停点。当您更改工作表上的某些内容时,该程序是否会触发?当 G1 发生变化时,它是否会继续执行 if...intersect 语句之后的部分?
    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多