vba PivotTable-应用程序定义的错误或对象定义的错误

时间:2019-02-11 07:07:17

标签: excel vba

我正在尝试使用excel VBA创建数据透视表。 我了解到这可能很棘手,建议使用R1C1引用样式将PivotRange调暗为String。 我已经编写了复制粘贴的代码,该代码可以在新的宏中使用,并且在下面指示的行上出现应用程序定义错误或对象定义错误。但是,“数据透视表”已经出现在工作表上,但是由于宏拒绝继续,因此显然未生成该字段。

问题:有人知道导致错误的原因吗?

代码:

    Option Explicit

Public wbTO As Workbook
Public ws As Worksheet
Public LastRow As Long, LastCol As Long

Sub CreatePivot()

Dim wbEFiche As Workbook
    Dim wsEF As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.AskToUpdateLinks = False

Set wbTO = ThisWorkbook
Set ws = wbTO.Sheets(1)
Set wsEF = wbTO.Worksheets("Result")

LastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol))

'Create Pivot table
    Dim PivotRange As String
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    'Due to an excel bug, the range has to be encoded in a string format, using a R1C1 reference style
    PivotRange = ws.Name & "!" & ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol)).Address(ReferenceStyle:=xlA1)
    'Set the pivot cache
    Debug.Print PivotRange
    'Debug.Print result = Sheet1!$A$1:$BH$168
    'ERROR ON THE FOLLOWING LINE
    Set PCache = wbTO.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PivotRange). _
    CreatePivotTable(TableDestination:=wsEF.Cells(12, 7), _
    TableName:="PostOccupationTable")
    'Create a blank pivot table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=wsEF.Cells(12, 7), TableName:="PostOccupationTable")
    'Insert Row & Column Fields
     With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
        .Orientation = xlRowField
        .Position = 1
    End With
    With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
        .Orientation = xlColumnField
        .Position = 1
    End With
    'Insert data field
    wsEF.PivotTables("PostOccupationTable").AddDataField ActiveSheet.PivotTables( _
        "PostOccupationTable").PivotFields("StamNr"), "Count of StamNr", xlCount
    'Set filters
    With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
        .PivotItems("(blank)").Visible = False
    End With
    With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
        .PivotItems("(blank)").Visible = False
    End With
    'Apply Style
    wsEF.PivotTables("PostOccupationTable").TableStyle2 = _
        "PivotStyleMedium2"

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.AskToUpdateLinks = True



End Sub

1 个答案:

答案 0 :(得分:0)

更改

Set PCache = wbTO.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PivotRange). _
    CreatePivotTable(TableDestination:=wsEF.Cells(12, 7), _
    TableName:="PostOccupationTable")

进入

Set PCache = wbTO.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PivotRange)

创建数据透视表在下一行完成,因此并不是必需的。显然造成了错误,我真的不知道为什么...