vba 如何访问 PivotCell.RowItem(n) 的字段名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9538406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the field name of a PivotCell.RowItem(n)?
提问by dbenham
I have an Exel 2007 PivotTable using classic layout that has 2 row fields. For a given data PivotCell, I want to loop through and identify the corresponding row field names and item values for that cell. My code works fine if I am on a sub-total or grand total line (0 or 1 items associated with the cell). But it fails if I try to work with a cell associated with 2 items - "Run-time error '1004': Application-defined or object-defined error".
我有一个 Exel 2007 数据透视表,使用具有 2 个行字段的经典布局。对于给定的数据 PivotCell,我想遍历并识别该单元格的相应行字段名称和项目值。如果我在小计或总计行(与单元格关联的 0 或 1 个项目)上,我的代码工作正常。但是,如果我尝试使用与 2 个项目关联的单元格,它会失败 - “运行时错误 '1004':应用程序定义或对象定义错误”。
The following line succeeds when i=1, but fails when i=2:
以下行在 i=1 时成功,但在 i=2 时失败:
cellSpec(1, i) = pvtCell.RowItems(i).LabelRange.PivotCell.PivotField.SourceName
When I set a watch expression to pvtCell, I see that:
当我将监视表达式设置为 pvtCell 时,我看到:
pvtCell.RowItems(2).LabelRange.PivotCell=<Application-defined or object-defined error>
Yet pvtCell.RowItems(2).Name
has the expected valid value.
但pvtCell.RowItems(2).Name
具有预期的有效值。
Anyone know a reliable method to get the name of the RowItem's field?
有人知道获取 RowItem 字段名称的可靠方法吗?
采纳答案by dbenham
I finally found the answer on a dedicated Excel forum :) http://www.mrexcel.com/forum/showthread.php?p=1638552#post1638552
我终于在一个专门的 Excel 论坛上找到了答案:) http://www.mrexcel.com/forum/showthread.php?p=1638552#post1638552
The code is now simpler, and it actually works!
代码现在更简单了,而且确实有效!
cellSpec(1, i) = pvtCell.RowItems(i).Parent.SourceName