VBA数据透视表访问列标题

时间:2018-08-27 14:58:55

标签: vba excel-2010

嗨,我在Windows 7上使用excel 2010 VBA。如何访问给定数据透视表的列标题。

例如下面的数据透视表

            Column Labels
            -------------
Row Labels  Col1          Col2         Col3
----------
Year 1      Value 1      Value 4      Value 7

Year 2      Value 2      Value 5      Value 8

Year 3      Value 3      Value 6      Value 9

我尝试访问col1,col2,col3,然后我想使用VBA中的数据透视表读取该给定列的所有值。我似乎无法抓取列标签?同样,一旦我检查了该列是否存在,我希望能够选择该给定列下的值。任何帮助都会很棒!

所以我要问的是如何检查并说col1作为一列存在,然后打印出值1、2和3。

1 个答案:

答案 0 :(得分:0)

我通常在创建数据透视表的过程下方添加此注释框。

'
' PIVOT Tables 
' These are created programatically.
'
' Relative references are used throughout.  Hard coded cell addresses are avoided.
' This allows it to continue to work with future data that expands/contracts either the rows or the columns.
'
' In this PIVOT Table example, there are six (6) ranges shown.
' By setting a variable equal to the range, and using REZISE and OFFSET functions,
'   any address can be developed relative to these ranges.
'         Dim rBody as Range
'   e.g.  rBody = ActiveSheet.PivotTables("PivotTable1").DataBodyRange
'         6   = rBody.Row
'         129 = rBody.Row + rBody.Rows.Count - 1
'          26 = rBody.Column + rBody.Columns.Count - 1
'         E6  = rBody.Resize(1,1)
'         AE6 = rBody.Resize(1,1).Offset(0,rBody.Columns.Count)
'
'/--------------------------------------------------\
'|    TABLE1 and TABLE2 refer to (A3:AD129)         |
'|                /--------------------------------\|
'|  A3            | E3                             ||
'|                |          COLUMN (E3:AD5)       ||
'|  A4            | E4                             ||
'| |------------\ |/------------------------------\||
'| |A5 B5 C5 D5 | ||E5       LABEL             AD5|||
'| |            | \================================/|
'| |            | /--------------------------------\|
'| |    ROW     | | E6                         AD6 || AE6
'| |            | |                                ||
'| |            | |           BODY                 ||
'| |            | |                                ||
'| |A129    D129| | E129                     AD129 ||
'| \------------/ \--------------------------------/|
'\--------------------------------------------------/