PivotCaches.Add Errors out

前端 未结 1 1931
猫巷女王i
猫巷女王i 2020-12-12 07:32

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

Set objExcel = CreateObject(\"Excel.Application\")
ob         


        
相关标签:
1条回答
  • 2020-12-12 08:18

    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:\Users\srujan\Desktop\TIME REPORT\fresh\25_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
    
    0 讨论(0)
提交回复
热议问题