【问题标题】:Error while creating pivot table: "Application Defined or object Defined Error."创建数据透视表时出错:“应用程序定义或对象定义错误。”
【发布时间】:2017-06-20 08:51:09
【问题描述】:

我正在创建一个数据透视表。我有三张工作表,我正在工作表CAT_Pivot 中创建数据透视表,并在准备工作表中使用数据。

我能够实现我的目标,但在执行结束时出现运行时错误。运行时错误状态

应用程序定义或对象定义错误。

除此之外,我如何计算数据透视表中的值?我用了.function = xlcount,没有成功。

这是我的代码:

Sub AutoPivot()

Dim PvtCache            As PivotCache
Dim PvtTbl              As PivotTable
Dim PvtSht              As Worksheet

' set Pivot Cache for Pivot Table
' Your range is static, there are ways to refer to a dynamic range
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Preparation sheet!R1C1:R1048576C8")

' set the Pivot table's sheet
Set PvtSht = Worksheets("CAT_Pivot")

' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PvtTbl = PvtSht.PivotTables("PivotTable1") ' check if "PivotTable7" Pivot Table already created (in past runs of this Macro)

On Error GoTo 0
If PvtTbl Is Nothing Then ' Pivot table object is nothing >> create it

    ' create a new Pivot Table in "PivotTable4" sheet
    Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("A3"), TableName:="PivotTable1")

    With PvtTbl
        With .PivotFields("Category")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("Colour")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Category")
            .PivotItems("DG-035583").Visible = False
            .PivotItems("DG-048917").Visible = False
            .PivotItems("DG-Series").Visible = False
            .PivotItems("gn").Visible = False
            .PivotItems("yl").Visible = False
            .PivotItems("(blank)").Visible = False
        End With
        With .PivotFields("Colour")
            .PivotItems("(blank)").Visible = False
        End With

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

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    问题在于您正在尝试编写嵌套语句。尝试从头开始重写代码,一次不要使用多个with。

    例如,这部分不好:

    With PvtTbl
        With .PivotFields("Category")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("Colour")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Category")
            .PivotItems("DG-035583").Visible = False
            .PivotItems("DG-048917").Visible = False
            .PivotItems("DG-Series").Visible = False
            .PivotItems("gn").Visible = False
            .PivotItems("yl").Visible = False
            .PivotItems("(blank)").Visible = False
        End With
        With .PivotFields("Colour")
            .PivotItems("(blank)").Visible = False
        End With
    
    End With
    

    你可以这样改写:

    With PvtTbl.PivotFields("Category")
        .Orientation = xlRowField
        .Position = 1
    End With
    With PvtTbl.PivotFields("Colour")
        .Orientation = xlColumnField
        .Position = 1
    End With
    

    嵌套 if 的参考 - Nested With Statement hiearchy

    干杯!

    编辑:实际上嵌套如果不是那么糟糕,它甚至可以工作。不过还是有点乱。

    【讨论】:

    • 谢谢。我会重写它并尝试。
    • @Mikz - 另外,尝试为您的问题建立一个较小的模型,如果您仍然遇到它,请发布它。例如,只有几个 PivotItems 和 PivotFields。
    • 我按照你建议的方法试过了,错误依然存在。
    • @Mikz - 你在哪一行得到它?将信息添加到问题中。
    • 表示您需要我的数据透视表的快照?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多