【问题标题】:Error 1004 when setting the name property of the PivotField class设置 PivotField 类的 name 属性时出现错误 1004
【发布时间】:2017-10-03 11:17:49
【问题描述】:

我创建了一个宏来自动化构建数据透视表的过程。宏运行良好,但它一直显示错误 1004。(但是,结果很好)。我在下面提供代码。

'PIVOT TABLE

Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PRange As Range
Dim LastRow12 As Long
Dim LastCol As Long
Set PSheet = ActiveSheet
LastRow12 = PSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = PSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = PSheet.Cells(1, 1).Resize(LastRow12, LastCol)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable TableDestination:=PSheet.Cells(2, 16), TableName:="PivotTable1"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Destination")
.Orientation = xlRowField
.Position = 1
.Subtotals(1) = True
.Subtotals(1) = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("End Date")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Trucks")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "0.0"
.Name = "Trucks"
End With

【问题讨论】:

  • 错误发生在哪一行?
  • 它没有给出一条线......它运行得很好......一切都完成后,它给出了一个消息框,说错误 1004(设置 PivotField 类的 name 属性时)。没有线没有什么......这就是为什么我无法解决它
  • 为什么它的名字已经是卡车的名字了?
  • 好点:p!那是错误...谢谢哥们!

标签: vba excel pivot pivot-table


【解决方案1】:

试试下面的代码,代码的 cmets 里面的解释:

Option Explicit

Sub CreatePivot4()

Dim pvt As PivotTable
Dim PvtCache As PivotCache

Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PRange As Range
Dim LastRow12 As Long
Dim LastCol As Long

Set PSheet = ActiveSheet
With PSheet
    LastRow12 = .Cells(.Rows.Count, 1).End(xlUp).Row
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Set PRange = .Range(.Cells(1, 1), .Cells(LastRow12, LastCol))
End With

' Create Pivot Cache
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange.Address(False, False, xlA1, xlExternal))

' Set the Pivot Table (already created in previous macro run)
On Error Resume Next
Set pvt = PSheet.PivotTables("PivotTable1")

On Error GoTo 0
If pvt Is Nothing Then ' <-- pivot table still doesn't exist >> need to create it

     ' create a new Pivot Table in ActiveSheet sheet, start from Cell A1
    Set pvt = PSheet.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PSheet.Cells(2, 16), TableName:="PivotTable1")

    With pvt
        With .PivotFields("Destination")
            .Orientation = xlRowField
            .Position = 1
            .Subtotals(1) = True
            .Subtotals(1) = False
        End With
        With .PivotFields("End Date")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Trucks")
            .Orientation = xlDataField
            .Position = 1
            .Function = xlSum
            .NumberFormat = "0.0"
            '.Name = "Trucks" ' * Why do you need to rename it ?
        End With
    End With
Else
    ' just refresh the Pivot table, with updated Pivot Cache
    pvt.ChangePivotCache PvtCache
    pvt.RefreshTable
End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多