+ Reply to Thread
Results 1 to 3 of 3

Creating a PivotTable in VBA Failing

Hybrid View

  1. #1
    Registered User
    Join Date
    07-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    54

    Creating a PivotTable in VBA Failing

    Hi there, I'm having a little bit of trouble creating a pivot table using VBA, and not sure why, given that I'm doing it as part of a function that's working fine on a different project and being called in exactly the same way?

    The macro fails on the
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Target.Range("A1:R" & LastRow))
    step
    with the error message: Method 'Add' of object 'PivotCaches' failed. I get a similar message if I try pivotcaches.create.
    any help would be much appreciated.
    here's the function:
    Function PivotTable(RecipientWksht As String, TargetWksht As String, PivotStRng As Range, TblNm As String, TblLbl As String) As String
    With Application
        ScreenUpdate = .ScreenUpdating
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    Dim LastRow, LastCol As Integer
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim i, j As Integer
    Dim Recipient, Target As Worksheet
    Dim PivotFinish As Range
    Set Recipient = Worksheets(RecipientWksht)
    Set Target = Worksheets(TargetWksht)
    
    LastRow = Target.Cells(Rows.Count, "A").End(xlUp).Row
    LastCol = Target.Cells(1, Columns.Count).End(xlToLeft).Row
    
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Target.Range("A1:R" & LastRow))
    Set PT = PTCache.CreatePivotTable(tablename:=TblNm, tabledestination:=PivotStRng)
    
                                    
    With Recipient.PT(TblNm)
        .EnableDrilldown = False
        .SaveData = False
        .ColumnGrand = False
        .RowGrand = False
        With .PivotFields("Account")
            .Orientation = xlRowField
            .Position = 1
        End With
        .AddDataField Recipient.PT(TblNm).PivotFields("Amount Due"), "Sum Of Amount Due", xlSum
        With .PivotFields("CCY")
            .Orientation = xlColumnField
            .Position = 1
        End With
    End With
    
    With PivotStRng
        .Offset(-1, 0) = TblLbl
        .Offset(0, 1) = "Currency"
        .Offset(1, 0) = "Fund"
    End With
    LastCol = PivotStRng.Offset(1, 0).End(xlToRight).Column
    LastRow = PivotStRng.End(xlDown).Row
    
    Set PivotFinish = Cells(LastRow, LastCol)
    Application.Names.Add TblNm, Range(PivotStRng.Offset(2, 1), PivotFinish)
    With Application
        .ScreenUpdating = ScreenUpdate
        .DisplayAlerts = True
    End With
    End Function
    Last edited by Cutter; 08-03-2012 at 10:21 AM. Reason: Removed OP request to delete duplicate thread

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Creating a PivotTable in VBA Failing

    Try changing the following line:

    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Target.Range("A1:R" & LastRow))
    To:

    Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Target.Range("A1:R" & LastRow))

  3. #3
    Registered User
    Join Date
    07-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    54

    Re: Creating a PivotTable in VBA Failing

    Tried that, same error message only with ".create" instead of ".add" :/

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1