vba 更改数据透视表的选择时如何自动运行宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10800134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:14:06  来源:igfitidea点击:

How to run a macro automatically when the selections of pivot tables are changed

excelexcel-vbapivot-tablevba

提问by harry

I got a pivot table with shows the data for Quarters in it, and a macro that performs some calculations based on the data in pivot table. So now I want to run the Macro automatically when I changed the selection of pivot items, like if select Quarter1 , it shows the values for Q1 and run the macro automatically so that it calculate the values for Q1, and same for all selections including the multiple selections..

我得到了一个数据透视表,其中显示了 Quarters 的数据,以及一个根据数据透视表中的数据执行一些计算的宏。所以现在我想在我更改数据透视项的选择时自动运行宏,比如如果选择 Quarter1 ,它会显示 Q1 的值并自动运行宏以便它计算 Q1 的值,并且所有选择都相同,包括多选。。

回答by Doug Glancy

Put something like this in the code module of the worksheet with the pivot table:

在带有数据透视表的工作表的代码模块中放置类似的内容:

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

' Call your macro here

End Sub