如何在 VBA 中为每个 table header 创建一个数据透视字段?

How to create a pivot field per table header in VBA?

对于我的数据透视表 table,我想为每个选定的 headers(例如“A”、“B”、“C”等)从我的来源创建一个数据字段 table。我没有为每个 header 编写单独的代码,而是通过 header 创建一个循环,因为我有超过 10 个 header。

我尝试的循环代码:

Dim h As Variant
Dim p As Long
Dim hdrs As Variant 'table headers of interest
hdrs = Array("A","B","C")

For Each h In hdrs
    With PSheet.PivotTables(PTName).PivotFields(hdrs)
    .Orientation = xlDataField
    .Function = xlAverage
    .NumberFormat = "0.00"
    .Position = p + 1
    End With
Next h

我的代码变量:

但是这段代码没有任何反应

这有效:

Dim hdrs As Variant
Dim i As Long
hdrs = Array("A", "B", "C")

For Each Item In hdrs
    PSheet.PivotTables(PTName).PivotFields (Item)
    With PSheet.PivotTables(PTName).PivotFields(Item)
    .Orientation = xlDataField
    .Function = xlAverage
    .NumberFormat = "0.00"
    .Position = i + 1
    End With
Next Item