简体   繁体   English

我可以在数据透视表的“值”象限中获取单个字段的名称吗?

[英]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.

相关问题 更改数据透视表中的字段名称 - Change Field name in a pivottable 如何获取此数据透视表以在所需的位置显示所需的值? - How can I get this PivotTable to display the values I want it to, in the location I want them to be? 如何让数据透视表列字段的成员按自然顺序显示,而不是按字母顺序显示? - How can I get the members of a PivotTable column field to display in natural order, rather than alphabetically? Excel中的数据透视表宏字段名称错误 - PivotTable Macro Field name error in Excel 我收到的运行时错误 1004 不一致:数据透视表字段名称无效 - I am inconsistently receiving Run-time error 1004: The PivotTable field name is not valid 宏无法获取正确的数据透视表名称 - Macro failing to get correct PivotTable name 如何提供基于Aspose.Cells数据透视表中其他两个值的计算值? - How can I provide a calculated value that is based on two other values in an Aspose.Cells PivotTable? 运行时错误“ 1004”:数据透视表字段名称无效 - Run-time error '1004': The PivotTable field name is not valid 在数据透视表中选择单元格将生成Sheet1。 如何获得创建命名标签的动作? - Selecting Cell in PivotTable produces Sheet1. How can I get this action to create a named tab? 如何使用 VBA 过滤一个或多个数据透视表字段? - How do I Filter one or multiple PivotTable Field using VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM