Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

excel - PivotCaches.Add Errors out

I'm using excel 2013 and I am getting invalid procedure call or argument while calling PivotCaches.Add

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set ActiveWorkbook = objExcel.Workbooks.Open("C:UserssrujanDesktopTIME REPORTfresh25_Report Time Booking_25.xls")
ActiveWorkbook.Sheets("25_Report Time Booking_25").Select


' Set the range to be pivoted to be called PivotRange

Set PivotTopLeft = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range("A1")
Set PivotTopRight = PivotTopLeft.Range("G1")
Set PivotTop = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range(PivotTopLeft, PivotTopRight)
Set MyPivotRange = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range(PivotTop, PivotTop.Range("G78"))


' Create the pivot table

'ActiveWorkbook.CutCopyMode = False

ActiveWorkbook.Parent.Worksheets.Add

MyPivotRangeName = "'" & MyPivotRange.Parent.Name & "'" & "!" & MyPivotRange.Address(ReferenceStyle = xlR1C1)
MsgBox (MyPivotRangeName)
Set MyPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType = xlDatabase, SourceData = MyPivotRangeName)

MyPivotCache.CreatePivotTable TableDestination = (ActiveWorkbook.Parent.Worksheets("Sheet1").Range("A3")), TableName = "PivotTable1"

ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotTableWizard

ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").SmallGrid = False

ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").AddFields RowFields = "Activity Type", PageFields = "User"

With ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Effort Spent (Hrs)" & Chr(10) & "($)")
    .Orientation = xlDataField
        .Function = xlSum
        .NumberFormat = "#,##0"
    End With

With ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Activity Type")
    .PivotItems("ANALYSIS").Visible = False
    .PivotItems("BUILD").Visible = False
    .PivotItems("CARRIERSAP").Visible = False
    .PivotItems("HOWTO").Visible = False
    .PivotItems("MAINTENANC").Visible = False
    .PivotItems("MAINTENANCE").Visible = False
    .PivotItems("REVIEW").Visible = False
    .PivotItems("STX-SCRIPT").Visible = False
    .PivotItems("(blank)").Visible = False
End With
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason why you are getting that error is because your are supposed to use .Create instead of .Add

Set MyPivotCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, MyPivotRangeName)

EDIT

Further to my comments under your question, you code could be optimized (UNTESTED) like this

Sub Sample()
    Dim wb As Workbook
    Dim ws As Worksheet, newWs As Worksheet
    Dim sFile As String, MyPivotRangeName As String
    Dim MyPivotRange As Range
    Dim pt As PivotTable
    Dim MyPivotCache As PivotCache

    sFile = "C:UserssrujanDesktopTIME REPORTfresh25_Report Time Booking_25.xls"

    Set wb = Workbooks.Open(sFile)
    Set ws = wb.Sheets("25_Report Time Booking_25")
    Set newWs = wb.Worksheets.Add

    With ws
        Set MyPivotRange = .Range("A1:G78")

        MyPivotRangeName = "'" & ws.Name & "'!" & MyPivotRange.Address(ReferenceStyle = xlR1C1)

        Set MyPivotCache = wb.PivotCaches.Create( _
                          SourceType = xlDatabase, _
                          SourceData = MyPivotRangeName)

        Set pt = MyPivotCache.CreatePivotTable( _
        tabledestination:=(newWs.Name & "!R3C1"), _
        tablename:="PivotTable1")

        '
        '~~> Rest of the code
        '
    End With
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...