我可以获取数据透视表 "Values" 象限中单个字段的名称吗?
Can I get the name of a single Field in the "Values" quadrant of PivotTable?
前言:
我正在尝试构建一个依赖于无绝对引用的宏。
问题:
我的部分宏需要活动数据透视表“值”象限中的字段名称。
字段名称将更改,因此我无法将其硬编码到我的宏中。 (在上图中是“AMT”,但在另一个数据透视表中可能是“Amount”,这就是为什么我需要它是动态的。)
如果我在值象限中只有一个字段,我如何才能将它的名称作为字符串获取?如果我可以将它的名称作为字符串获取,我可以将其输入到我拥有的宏中。
说到这里,我的代码仅供参考:
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
您可以在大约一半的地方看到我放了一条注释,显示需要动态引用名称的 PivotFields 对象。我不希望代码读取“(AMT 总和)”,而是读取“([FieldName] 总和)”,其中 [FieldName] 是随数据透视表变化的动态字段名称字符串。
在我使用这个宏的绝大多数时间里,“值”象限中只有一个字段。如果很容易使宏可扩展到值象限中的多个字段,那是一个好处,但绝对不是必需的或不需要的。
我知道数据透视表 vba 很复杂,因此非常感谢您为 table!
带来的任何宝贵见解
谢谢,
洛根
这可能会让您入门:
Dim f As PivotField
For Each f In ActiveSheet.PivotTables(1).DataFields
Debug.Print f.SourceName, f.Name
Next f
另请参阅:https://www.contextures.com/excel-vba-pivot-table-field-list.html
前言:
我正在尝试构建一个依赖于无绝对引用的宏。
问题:
我的部分宏需要活动数据透视表“值”象限中的字段名称。
字段名称将更改,因此我无法将其硬编码到我的宏中。 (在上图中是“AMT”,但在另一个数据透视表中可能是“Amount”,这就是为什么我需要它是动态的。)
如果我在值象限中只有一个字段,我如何才能将它的名称作为字符串获取?如果我可以将它的名称作为字符串获取,我可以将其输入到我拥有的宏中。
说到这里,我的代码仅供参考:
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
您可以在大约一半的地方看到我放了一条注释,显示需要动态引用名称的 PivotFields 对象。我不希望代码读取“(AMT 总和)”,而是读取“([FieldName] 总和)”,其中 [FieldName] 是随数据透视表变化的动态字段名称字符串。
在我使用这个宏的绝大多数时间里,“值”象限中只有一个字段。如果很容易使宏可扩展到值象限中的多个字段,那是一个好处,但绝对不是必需的或不需要的。
我知道数据透视表 vba 很复杂,因此非常感谢您为 table!
带来的任何宝贵见解谢谢, 洛根
这可能会让您入门:
Dim f As PivotField
For Each f In ActiveSheet.PivotTables(1).DataFields
Debug.Print f.SourceName, f.Name
Next f
另请参阅:https://www.contextures.com/excel-vba-pivot-table-field-list.html