vba - 访问列标题的 VBA 数据透视表
问题描述
嗨,我在 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。
解决方案
我通常在创建数据透视表的过程下方添加此注释框。
'
' 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 ||
'| \------------/ \--------------------------------/|
'\--------------------------------------------------/
推荐阅读
- angular - 我正在尝试在角度 js 中显示 keyName 、 Keyless 和 Keypresent 对象数组的模式,但它不起作用
- python - 无法安装 spyder 以在 Manjaro 上运行
- php - 共享主机中的 Laravel 7.x 托管问题
- python-3.x - 如何使用 python3 pandas 数据帧执行广度优先搜索
- java - 如何将条目添加到数据库表以组合两个表
- jquery - 如何在 Laravel 中降级 jquery 版本
- amazon-sagemaker - Sagemaker 默认推理处理程序使用“无”转换器初始化
- google-chrome - 无法使用 Chrome 登录 Dot Net Core 3 ASP.Net App,但可以使用 Firefox
- python - Problem with file management (open command not recognized)
- jekyll - 如何从 Jekyll _site 文件夹中删除意外文件