繁体   English   中英

在VBA中引用Excel 2012中的数据透视表

[英]Referencing pivot tables from excel 2012 in VBA

我现在正在处理一个引用数据透视表的宏,但是我遇到的问题是我的引用仅针对特定的单元格范围,随着它的增长或缩小,我无法使其引用整个表数据。 我尝试了几种不同的方法,但是我没有使用VB的经验,所以不确定我是否完全理解语法。

这是我目前拥有的:

Sheets("Loader").Select
Range("C11").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "1"
Sheets("Worksheet").Select
Range("U4:Y10").Select
Selection.Copy

参考“ U4:Y10”应该是数据透视表中的信息

将Range.PivotTable.x用作数据透视表中值的范围,其中x是从下面的示例中显示的有效范围中进行选择。

例如,在您的情况下,使用单元格U4作为锚点单元格,从中查找包含它的数据透视表:Debug.Print Range(“ U4”)。PivotTable.TableRange1.Address(打印例如“ U4:Y30”)

您想要的范围有多种选择,例如:

With Range("U4").PivotTable
    .DataBodyRange 'Range of just the values
    .ColumnRange 'Range of Column 'labels'
    .RowRange 'Range of Row 'labels'
    .TableRange1 'Range of entire table (excluding page fields)
    .TableRange2 'Range of entire table (including page fields)
End With

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM