数据透视和运行时错误1004:应用程序定义或对象定义的错误 [英] Pivots and Run-time error 1004: Application-defined or object-defined error

查看:325
本文介绍了数据透视和运行时错误1004:应用程序定义或对象定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢宏和VBA编码,而我正在尝试创建一个非常简单的宏,它从33个列的表中获取数据,并将其转换为一个枢纽。



只有最后3列(31,32,33)包含数字,我需要显示在枢轴上,因为我想比较当前月份,上个月和月份的变化。



这是我的代码到目前为止:

  Sub Macro6()
'
'Macro6宏
'

'
Dim shtSrc As Worksheet,shtDest As Worksheet
Dim pc As PivotCache

设置shtSrc = ActiveSheet

设置shtDest = shtSrc.Parent.Sheets.Add()
shtDest.Name = shtSrc.Name& -Pivot

设置pc = ActiveWorkbook.PivotCaches.Create(SourceType:= xlDatabase,_
SourceData:= shtSrc.Range(A1)。CurrentRegion)
pc .CreatePivotTable TableDestination:= shtDest.Range(A3),_
TableName:=PivotTable1

带有shtDest.PivotTables(PivotTable1)
.InGridDropZones = True
.RowAxisLayout xlTabularRow
结束

设置shtDest = ActiveSheet

使用ActiveSheet.PivotTables(PivotTable1)。PivotFields(Name)
.Orientation = xlRowField
.Position = 1
结束

ActiveSheet.PivotTables(PivotTable1)。AddDataField ActiveSheet.PivotTables(_
PivotTable1 ).PivotFields(31),上个月,xlSum
ActiveSheet.PivotTables(PivotTable1)。AddDataField ActiveSheet.PivotTables(_
PivotTable1)。PivotFields(32),这个月,xlSum
ActiveSheet.PivotTables(PivotTable1)。AddDataField ActiveSheet。数据透视表(_
PivotTable1)。PivotFields(33),Movement,xlSum



End Sub

我收到错误运行时错误1004:此行上的应用程序定义或对象定义的错误:

  ActiveSheet.PivotTables(PivotTable1)。AddDataField ActiveSheet.PivotTables(_ 
PivotTable1)。PivotFields(33),Movement,xlSum

如果我删除这一行,宏可以正常工作,但只创建2列。如果我放在一起,它会生成错误,不会创建第3列。



任何人都可以解释为什么?

解决方案

好的。我想我想出来了,但是这是一件很奇怪的事情,这是一个bug吗?



原因是在我的源表中,列标题表示运动。如果我将标题更改为列标题以外的其他内容,通过说运动,则宏可以工作。

  ActiveSheet.PivotTables(PivotTable1)。AddDataField ActiveSheet.PivotTables(_ 
PivotTable1)。PivotFields(33),Movements,xlSum


I'm quite new to macros and VBA coding and I'm trying to create a really simple macro that takes the data from a table with 33 columns and convert it to a pivot.

Only the last 3 columns (31, 32, 33) contains numbers and I need that to appear on the pivot as I want to compare current month, last month and the movement from month on month.

Here is my code so far:

Sub Macro6()
'
' Macro6 Macro
'

'
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim pc As PivotCache

    Set shtSrc = ActiveSheet

    Set shtDest = shtSrc.Parent.Sheets.Add()
    shtDest.Name = shtSrc.Name & "-Pivot"

    Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=shtSrc.Range("A1").CurrentRegion)
    pc.CreatePivotTable TableDestination:=shtDest.Range("A3"), _
        TableName:="PivotTable1"

    With shtDest.PivotTables("PivotTable1")
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
    End With

    Set shtDest = ActiveSheet

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With

    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields(31), "Last month", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields(32), "This month", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields(33), "Movement", xlSum



End Sub

I get an error Run-time error 1004: Application-defined or object-defined error on this line:

ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields(33), "Movement", xlSum

If I take away this line, the macro works fine but only creates 2 column. If I put in the line, it generates the error and does not create the 3rd column.

Can anyone explain why?

解决方案

OK. I think I figured it out, but it is quite strange that this is happening, is it a bug?

The reason is that in my source table, the column heading says "Movement". If I change the caption to something other than the Column heading for example, by saying "MovementS", then the macro works.

 ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields(33), "Movements", xlSum

这篇关于数据透视和运行时错误1004:应用程序定义或对象定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆