Excel使用VBA迭代嵌套的数据透视表行字段

时间:2022-09-15 20:51:04

I have a Pivot Table and I need to iterate through nested RowFields:

我有一个数据透视表,我需要迭代嵌套的RowFields:

Excel使用VBA迭代嵌套的数据透视表行字段

How can I parse the nested RowFields?

如何解析嵌套的RowFields?

My code so far is:

到目前为止我的代码是:

Sub PtRead()
    Dim i As Integer, j As Integer
    Dim Sh As Worksheet
    Dim pt As PivotTable
    Dim rngRow As Range

    Set Sh = Sheets("Store")
    Set pt = Sh.PivotTables(1)

    i = 3
    For j = 1 To pt.RowFields(i).PivotItems.Count
        Debug.Print pt.RowFields(i).PivotItems(j)
    Next

End Sub

I need to retrieve, for example, all the nested article codes related to brand "AAA" and the related Nsum value with something like (this doesn't work..):

例如,我需要检索与品牌“AAA”相关的所有嵌套商品代码以及相关的Nsum值(这不起作用..):

...    
pt.RowFields(1).PivotItems(1).RowFields(2).PivotItems(j)
...

1 个解决方案

#1


0  

I think you want to loop through the Pivot Items. Look at the code below for some guidance on how to proceed.

我想你想要遍历Pivot项目。请查看下面的代码,了解有关如何继续的一些指导。

Sub TryMe()

For Each Pi In ActiveSheet.PivotTables("PivotTable1").PivotFields("DeptHead").PivotItems
    If Pi.Visible Then
        Count = Count + 1
        Range("B" & Count).Value = Pi.Name
        Row = Row + 1
    End If
Next Pi

End Sub

The Macro Recorder is very helpful if you get stuck on something.

如果您遇到问题,Macro Recorder非常有用。

#1


0  

I think you want to loop through the Pivot Items. Look at the code below for some guidance on how to proceed.

我想你想要遍历Pivot项目。请查看下面的代码,了解有关如何继续的一些指导。

Sub TryMe()

For Each Pi In ActiveSheet.PivotTables("PivotTable1").PivotFields("DeptHead").PivotItems
    If Pi.Visible Then
        Count = Count + 1
        Range("B" & Count).Value = Pi.Name
        Row = Row + 1
    End If
Next Pi

End Sub

The Macro Recorder is very helpful if you get stuck on something.

如果您遇到问题,Macro Recorder非常有用。