[英]Can I get the name of a single Field in the "Values" quadrant of PivotTable?
Preface:前言:
I am trying to build a macro that relies on no absolute references.我正在尝试构建一个不依赖于绝对引用的宏。
Problem:问题:
Part of my macro needs the name of the field in the "Values" quadrant of the active PivotTable.我的宏的一部分需要活动数据透视表的“值”象限中的字段名称。
The field name will change so I cannot hard code it into my macro.字段名称将更改,因此我无法将其硬编码到我的宏中。 (it is "AMT" in the image above, but it might be "Amount" in another PivotTable, which is why I need it to be dynamic.)
(上图中是“AMT”,但在另一个数据透视表中可能是“Amount”,这就是为什么我需要它是动态的。)
If I have a single Field in the value quadrant, how can I get it's name as a string?如果我在值象限中有一个字段,如何将其名称作为字符串获取? If I can just get it's name as a string, I can feed that into the macro that I have.
如果我可以将它的名称作为字符串获取,我可以将其输入到我拥有的宏中。
Speaking of which, here is my code just for reference:说到这里,我的代码仅供参考:
Sub PivotNumberFormat()
'PivotNumberFormat Macro
'Formats the "Values" data field in the currently active pivot to: number format, 2 decimals, and commas
'=============================================
'Define variables
Dim PT As PivotTable
'=============================================
'Continue if pivot table not selected
On Error GoTo EndSub
'=============================================
'Set pivot table as variable "PT"
Set PT = ActiveCell.PivotTable
'=============================================
'If pivot table selected, change number format
If Not PT Is Nothing Then
'Change the format of the pivot table amt field
With PT.PivotFields("Sum of AMT") '<-------------This is the line where I need a dynamic reference.
.NumberFormat = "#,##0.00_);(#,##0.00)"
End With
'Notify user that format was changed
MsgBox "Format Changed"
'Stop macro
Exit Sub
End If
'=============================================
'If error, notify user and stop macro
EndSub:
MsgBox "Format NOT Changed"
End Sub
You can see about halfway down I put a note showing the PivotFields object that needs a dynamically-referenced name.您可以看到大约一半,我放了一个注释,显示需要动态引用名称的 PivotFields object。 Instead of the code reading "(Sum of AMT)", I want it to read "(Sum of [FieldName])" with [FieldName] being the dynamic field name string that changes with the PivotTable.
而不是读取“(AMT 的总和)”的代码,我希望它读取“([FieldName] 的总和)”,其中 [FieldName] 是随数据透视表更改的动态字段名称字符串。
The vast majority of the time that I will use this macro, there will only be one field in the Values quadrant.在我使用这个宏的绝大多数时间里,值象限中只有一个字段。 If it is easy to make the macro scalable to multiple fields in the Values quadrant, that is a bonus, but definitely not required or needed.
如果很容易使宏扩展到值象限中的多个字段,那是一个好处,但绝对不是必需的或不需要的。
I know that PivotTable vba is complex so I appreciate any of your valuable insight you bring to the table!我知道数据透视表 vba 很复杂,因此我感谢您提出的任何宝贵见解!
Thanks, Logan谢谢,洛根
This might get you started:这可能会让你开始:
Dim f As PivotField
For Each f In ActiveSheet.PivotTables(1).DataFields
Debug.Print f.SourceName, f.Name
Next f
See also: https://www.contextures.com/excel-vba-pivot-table-field-list.html另请参阅: https://www.contextures.com/excel-vba-pivot-table-field-list.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.