【问题标题】:Pivot Table VBA Error from Recorded Macro来自录制宏的数据透视表 VBA 错误
【发布时间】:2017-01-25 22:22:08
【问题描述】:

以下是我对代码的更新。 TableX 是我的数据透视表将使用的引用表。在将“shtReport”变量声明为工作表并进一步命名变量后,会出现错误“对象变量或未设置块变量”。

Option Explicit

    Sub SutoPivot()

    Dim PvtTbl                          As PivotTable
    Dim PvtCache                        As PivotCache
    Dim shtReport                       As Worksheet

    ' set the Pivot Cache  (NOT SURE what is "TableX" ?)
    Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="TableX", Version:=xlPivotTableVersion14)
    Set shtReport = Worksheets("Invoice Data")

    ' add this line in case the Pivot table doesn't exit >> first time running this Macro
    On Error Resume Next
    Set PvtTbl = Worksheets("Invoice Data").PivotTables("PivotTable1a")

    On Error GoTo 0
    If PvtTbl Is Nothing Then
        ' create a new Pivot Table in "Invoice Data" sheet, start from Cell Y1
        Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=Worksheets("Invoice Data").Range("Y1"), _ TableName:="PivotTable1a")

        With PvtTbl.PivotFields("Sales Director")
            .Orientation = xlRowField
            .Position = 1
        End With

        With PvtTbl.PivotFields("Manager")
            .Orientation = xlRowField
            .Position = 2
        End With

        With PvtTbl.PivotFields("Owner")
            .Orientation = xlRowField
            .Position = 3
        End With

        With PvtTbl.PivotFields("Account Name")
            .Orientation = xlRowField
            .Position = 4
        End With

        With PvtTbl.PivotFields("Business Name")
            .Orientation = xlRowField
            .Position = 5
        End With

        With PvtTbl.PivotFields("Annual Aggregate Volume")
            .Orientation = xlValuesField
            .Position = 1
        End With

        PvtTbl.AddDataField PvtTbl.PivotFields("Annual Aggregate Volume"),
            "Sum of Annual Aggregate Volume", xlSum

        With PvtTbl.PivotFields("Sum of Annual Aggregate Volume")
            .NumberFormat = "#,##0"
        End With

    Else
        ' just refresh the Pivot cache with the updated Range
        PvtTbl.ChangePivotCache PvtCache
        PvtTbl.RefreshTable
    End If

    End Sub

【问题讨论】:

  • 错误出现在哪一行?宏的开始在哪里?
  • 您好 BruceWayne,错误从代码的第一部分开始,这也是宏开始的地方。我记录了这个宏并试图在我的数据中实现记录。它给了我这个错误。
  • @J.Woe 你在我下面的答案中测试过代码吗?它对你有用吗?
  • 嗨 Shai Rado,我非常感谢您的跟进。我实现了您的代码,但在突出显示“shtReport.”时出现“未定义变量”错误。我尝试修复它并想出了这行代码,在原始帖子中进行了编辑以供您监督。它现在给了我一个“对象变量或未设置块变量”错误。再次感谢,感谢您的周到。

标签: vba excel pivot-table


【解决方案1】:

下面的代码将帮助您开始在 VBA 中自动创建和更新PivotTables

Option Explicit

Sub SutoPivot()

Dim PvtTbl                          As PivotTable
Dim PvtCache                        As PivotCache

' set the Pivot Cache  (NOT SURE what is "TableX" ?)
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="TableX", Version:=xlPivotTableVersion14)

' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PvtTbl = Worksheets("Invoice Data").PivotTables("PivotTable1a")

On Error GoTo 0
If PvtTbl Is Nothing Then
    ' create a new Pivot Table in "Invoice Data" sheet, start from Cell Y1
    Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=Worksheets("Invoice Data").Range("Y1"), TableName:="PivotTable1a")

    With PvtTbl.PivotFields("Director")
        .Orientation = xlRowField
        .Position = 1
    End With

    With PvtTbl.PivotFields("Manager")
        .Orientation = xlRowField
        .Position = 2
    End With

    With PvtTbl.PivotFields("Owner")
        .Orientation = xlRowField
        .Position = 3
    End With

    With PvtTbl.PivotFields("Account Name")
        .Orientation = xlRowField
        .Position = 4
    End With

    With PvtTbl.PivotFields("Business Name")
        .Orientation = xlRowField
        .Position = 5
    End With

    PvtTbl.AddDataField PvtTbl.PivotFields("Annual Aggregate Volume"), _
        "Sum of Annual Aggregate Volume", xlSum

    With PvtTbl.PivotFields("Sum of Annual Aggregate Volume")
        .NumberFormat = "#,##0"
    End With

Else
    ' just refresh the Pivot cache with the updated Range  
    PvtTbl.ChangePivotCache PvtCache
    PvtTbl.RefreshTable
End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多