【问题标题】:Excel iterate through nested pivot table rowfields with VBAExcel 使用 VBA 遍历嵌套的数据透视表行字段
【发布时间】:2012-12-17 22:36:18
【问题描述】:

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

如何解析嵌套的 RowFields?

到目前为止我的代码是:

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

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

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

【问题讨论】:

    标签: excel pivot-table vba


    【解决方案1】:

    我认为您想循环浏览枢轴项。查看下面的代码以获取有关如何继续的一些指导。

    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
    

    如果您遇到问题,宏记录器会非常有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2014-12-24
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      相关资源
      最近更新 更多