【问题标题】:Pivot Table Expand "Values" VBA数据透视表展开“值”VBA
【发布时间】:2016-11-18 00:42:19
【问题描述】:

我正在尝试将字段自动添加到已创建的数据透视表中。这些字段将是“期间 1 的总和”...“期间 360 的总和”。我已经有 1-60 的时期,但缺少 61-360。手动往下拖300次会很痛苦。所以我写了一个宏,但我得到了错误: 无法获取枢轴字段类的枢轴项属性

由于已经创建了数据透视表(这是一个包含许多格式怪癖的大表),我不想更改任何内容。我只想将周期添加为 period61-period360 的值的总和。

我已附上我的代码。任何人可以给我的任何帮助都将非常感激,因为我不知道我做错了什么,而且我尽量不要太沮丧。

非常感谢您。

这是我的代码:

'--------------------- Expand_Pivot_to_360M Macro-------------------'

Sub Expand_Pivot_to_360M()

' Setting Pivot field classes to the "unable to get pivot items property of the pivot field class" error
    Sheets("Monthly Summary").Select

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("universe")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("buckets_break")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("pool_name")
        .Orientation = xlRowField
        .Position = 3
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("gl_company_code")
        .Orientation = xlRowField
        .Position = 4
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("LEP")
        .Orientation = xlRowField
        .Position = 5
    End With

循环扩展 Pivit 36​​0 周期

' Using a Loop to Expand Pivot for 360 periods
    Dim i As Integer

        For i = 61 To 360       ' start at period 61 since periods 1 to 60 already include in pivot

            Sheets("Monthly Summary").Select
            ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
                "PivotTable1").PivotFields("Period_i"), "Sum of Period_i", xlSum
            With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Period_i")
                .Caption = "Count of Period_i"
                .Function = xlCount
            End With
            With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Period_i")
                .Caption = "Sum of Period_i"
                .Function = xlSum
            End With

        Next i

End Sub

【问题讨论】:

  • 用_" & i替换_i"
  • 谢谢,我这样做了,不再出现“无法获取数据透视项属性...”错误,但我确实收到另一个错误:应用程序定义或对象定义错误”。请让我知道你能不能帮忙,有什么办法可以解决这个问题吗?谢谢

标签: vba excel for-loop pivot-table


【解决方案1】:

您有很多从宏录制中生成的无关代码。此外,您可能会收到“应用程序定义或对象定义错误”消息,因为您尝试添加的一个或多个字段已经存在。

我会建议这个...

Dim i as Integer
Dim found as Boolean
Dim aPItem as PivotItem
With Sheets("Monthly Summary").PivotTables("PivotTable1")
    For i = 61 to 360
        found = false
        For Each aPItem In .DataPivotField.PivotItems 'Check field not already there
            If aPItem.Caption = "Sum of Period_" & i Then
                found = True
                Exit For
            End If
        Next aPItem

        If Not found then 'add field
            .AddDataField .PivotFields("Period_" & i), "Sum of Period_" & i, xlSum
        End If
    Next i
End With

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2017-02-08
    相关资源
    最近更新 更多