[英]How would I use VBA to automatically populate an excel worksheet with data from the pivot table of a separate excel workbook?
I have two open Excel workbooks. 我有两个打开的Excel工作簿。 One has some data in a pivot table, like so: 一个在数据透视表中有一些数据,如下所示:
Row Labels Date
A 5
B 4
C 3
The other, separate workbook is blank other than Column A, which lists the same Row Labels as are in the pivot table, but possibly in a different order: 除列A以外,另一个单独的工作簿为空白,其中列A与数据透视表中的行标签相同,但顺序可能不同:
Row Labels 行标签
B 乙
A 一种
C C
I would like to place a button on the second workbook that, when pushed, will automatically populate the row labels with the appropriate columns from the pivot table, in the correct order. 我想在第二个工作簿上放置一个按钮,当按下该按钮时,它将以正确的顺序自动使用数据透视表中的相应列填充行标签。
I know that VLOOKUP will be involved somehow, but I am not quite sure how to get there. 我知道VLOOKUP会以某种方式参与其中,但是我不确定如何到达那里。 Ideally, it would be nice if this VBA code could be dynamic, so that the button would work no matter the size of the pivot table, and no matter how many row labels will be in the second workbook. 理想情况下,如果此VBA代码可以动态运行,那就太好了,这样无论透视表的大小如何,第二个工作簿中有多少行标签,该按钮都可以工作。
Use the GETPIVOTDATA
function - for example: 使用GETPIVOTDATA
函数-例如:
=GETPIVOTDATA("Date",[PivotWorkbook]Sheet1!$A$1,"Row Field",$A2)
The workbook containing the Pivot Table must be open to calculate the result. 必须打开包含数据透视表的工作簿才能计算结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.