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

时间:2013-08-12 13:46:47

标签: excel vba excel-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”引用应该是数据透视表中的信息

1 个答案:

答案 0 :(得分:1)

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

E.g。在你的情况下使用Cell 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