简体   繁体   English

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

[英]Referencing pivot tables from excel 2012 in VBA

I am working on a macro right now that references a pivot table but the problem I'm having is that my reference is only for a specific cell range and I can't get it to reference the entire table as it grows or shrinks depending on the data. 我现在正在处理一个引用数据透视表的宏,但是我遇到的问题是我的引用仅针对特定的单元格范围,随着它的增长或缩小,我无法使其引用整个表数据。 I've tried a few different things but I have no experience with VB so I'm not sure I entirely understand the syntax... 我尝试了几种不同的方法,但是我没有使用VB的经验,所以不确定我是否完全理解语法。

This is what I have currently: 这是我目前拥有的:

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

The "U4:Y10" reference should be the information within the pivot table 参考“ U4:Y10”应该是数据透视表中的信息

Use Range.PivotTable.x for the Range of the values within the pivot table where x is a choice from the valid ranges shown in my example below. 将Range.PivotTable.x用作数据透视表中值的范围,其中x是从下面的示例中显示的有效范围中进行选择。

Eg in your case use Cell U4 as the anchor cell from which to find the Pivot Table that contains it: Debug.Print Range("U4").PivotTable.TableRange1.Address (prints eg "U4:Y30") 例如,在您的情况下,使用单元格U4作为锚点单元格,从中查找包含它的数据透视表:Debug.Print Range(“ U4”)。PivotTable.TableRange1.Address(打印例如“ U4:Y30”)

The various choices over which range you want are such as: 您想要的范围有多种选择,例如:

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