错误 440:对象“PivotCaches”的方法“创建”失败

2024-01-29

我一直在尝试创建一个填充数据透视表的宏。然而,我不断得到这个

运行时错误 440

on the Set Cache line.

以前,我遇到过其他运行时错误,但这些都很容易修复。我不明白为什么PivotCaches.Create在这种情况下不允许使用该方法。 SOS,请帮忙!

这是代码:

' Declare variables for pivots
Dim pivotWB As Workbook
Dim RawDataWS As Worksheet
Dim dataLocation As Range
Dim cache As PivotCache
Dim table As PivotTables

' name and set the workbook for pivot tables
folderName = ThisWorkbook.Sheets(1).Cells(5, 13).Value
fileName = ThisWorkbook.Sheets(1).Cells(6, 13).Value
extName = ThisWorkbook.Sheets(1).Cells(7, 13).Value
wbAddress = folderName & "\" & fileName & "." & extName
Set pivotWB = Workbooks.Open(wbAddress)

Set dataLocation = pivotWB.Sheets("Raw Data").Range("A:AK")

Set cache = pivotWB.PivotCaches.Create(SourceType:=xlDatabase,_
sourceData:=dataLocation, Version:=xlPivotTableVersion14)

For i = 1 To 5

' create new worksheet and pivot table
pivotWB.Sheets.Add Before:=Sheets(1)
pivotWB.Sheets(1).Name = Left(toPivot(i), 30)
cache.CreatePivotTable TableDestination:=pivotWB.Sheets(1).Cells(1, 1), TableName:=toPivot(i) & " Pivot Table"

' set rows
With pivotWB.Sheets(1).PivotTables(toPivot(i) & " Pivot Table").PivotFields("Rejection Category Description")
    .Orientation = xlRowField
    .Position = 1
End With

' set columns
With pivotWB.Sheets(1).PivotTables(toPivot(i) & " Pivot Table").PivotFields("Post Period")
    .Orientation = xlColumnField
    .Position = 1
End With

' set values
With pivotWB.Sheets(1).PivotTables(toPivot(i) & " Pivot Table").PivotFields("Procedure Code")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlCount
    .NumberFormat = "#,##0"
    .Name = "Count of Code"
End With
With pivotWB.Sheets(1).PivotTables(toPivot(i) & " Pivot Table").PivotFields("Amount")
    .Orientation = xlDataField
    .Position = 2
    .Function = xlSum
    .NumberFormat = "$#,##0"
    .Name = "Sum of Amount"
End With

Next i

在 @Rory 评论之后,您需要设置PivotCache对象使用Address财产。

另外,设置你的table反对你的PivotTable将创建更简单、更短的代码。

请参阅下面的代码,代码注释中包含更多注释。

修改代码

' --- set the Pivot Cache ---
Set cache = pivotWB.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:=dataLocation.Address(False, False, xlR1C1, xlExternal))

For i = 1 To 5
    ' create new worksheet and pivot table
    pivotWB.Sheets.Add Before:=Sheets(1)
    pivotWB.Sheets(1).Name = Left(toPivot(i), 30)

    ' --- set the Pivot-Table Object ---
    Set table = cache.CreatePivotTable(TableDestination:=pivotWB.Sheets(1).Cells(1, 1), TableName:=toPivot(i) & " Pivot Table")

    With table ' <-- after you set the table object, you can simply use With statement now

        ' set rows
        With .PivotFields("Rejection Category Description")
            .Orientation = xlRowField
            .Position = 1
        End With

        ' set columns
        With .PivotFields("Post Period")
            .Orientation = xlColumnField
            .Position = 1
        End With

        ' set values
        With .PivotFields("Procedure Code")
            .Orientation = xlDataField
            .Position = 1
            .Function = xlCount
            .NumberFormat = "#,##0"
            .Name = "Count of Code"
        End With
        With .PivotFields("Amount")
            .Orientation = xlDataField
            .Position = 2
            .Function = xlSum
            .NumberFormat = "$#,##0"
            .Name = "Sum of Amount"
        End With
    End With
Next i
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误 440:对象“PivotCaches”的方法“创建”失败 的相关文章

随机推荐