0

I have done a fair amount of research on how to add a pivot table to another workbook but am coming up short.

My Data Set

Macro is stored in ThisWorkbook

Source Data for PivotTable is stored in the Open Workbook - Sheet name "Raw Data"

Destination of PivotTable wanted on the same Open Workbook but on Sheet name "Claim Summary"

Caveat

This procedure is pat of a much wider project. As such I have a few module declarations:

Private wbReconcile As Workbook

Private wsRawData As Worksheet

Private wsSummary As Worksheet

These are then set in a differnet procedure (within the same module) as such:

Set wbReconcile = Workbooks.Open(RECON_FILE, False)

Set wsRawData = wbReconcile.Sheets("Raw Data")

Set wsSummary = wbReconcile.Sheets("Claim Summary")

Problem & Error

The problem I have is that no matter if I use my variables or type out the name of the workbook, name of the worksheet/s etc.. I always get an error on the line Set myPivotTable Error Message: Run-time error '5': Invalid procedure call or argument.

My Code

Sub CreatePivotTable()

    Dim myDestinationWorkbook As Workbook
    Set myDestinationWorkbook = Workbooks("Template_Promo Claims Reconciliation.xlsm")
'    Set myDestinationWorkbook = wbReconcile

    Dim mySourceWorksheet As Worksheet
    Set mySourceWorksheet = myDestinationWorkbook.Worksheets("Raw Data")
'    Set mySourceWorksheet = wsRawData
    Dim myDestinationWorksheet As Worksheet
    Set myDestinationWorksheet = myDestinationWorkbook.Worksheets("Claim Summary")
'    Set myDestinationWorksheet = wsSummary

    'obtain address of destination cell range
    Dim myDestinationRange As String
    myDestinationRange = myDestinationWorksheet.Range("A1").Address(ReferenceStyle:=xlR1C1)

    Dim mySourceData As String
    mySourceData = mySourceWorksheet.Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)

    'create Pivot Table cache and create Pivot Table report based on that cache
    Dim myPivotCache As PivotCache
    Set myPivotCache = _
        myDestinationWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="[" & myDestinationWorkbook.name & "]" & _
        mySourceWorksheet.name & "!" & _
        mySourceData)

    Dim myPivotTable As PivotTable
    Set myPivotTable = myPivotCache.CreatePivotTable(TableDestination:="[" & _
        myDestinationWorkbook.name & "]" & myDestinationWorksheet.name & "!" & _
        myDestinationRange, TableName:="RawDataPivot")

End Sub

EDIT TO SHOW HOW ANSWER IS USED

Sub AnswerPT()

    Dim pc As PivotCache, pt As PivotTable
    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim rngSrc As Range, rngDest As Range

    Set wsSrc = wsRawData
    Set wsDest = wsSummary   'for convenience here

    Set rngSrc = wsSrc.Range("A1").CurrentRegion  'source data
    Set rngDest = wsDest.Range("A1")    'table location

    Set pc = wsDest.Parent.PivotCaches.Create(SourceType:=xlDatabase, _
                            SourceData:=rngSrc, Version:=6)

    Set pt = pc.CreatePivotTable(TableDestination:=rngDest, _
                          TableName:="PivotTable2", DefaultVersion:=6)

End Sub
6
  • 1
    What is the error message?
    – BigBen
    Jan 31, 2020 at 1:40
  • updated post: Error Message: Run-time error '5': Invalid procedure call or argument. Jan 31, 2020 at 1:42
  • 1
    If myDestinationWorksheet.Name contains spaces the sheet name needs to be in single quotes. No harm in always adding single quotes in this type of case. Jan 31, 2020 at 1:42
  • @TimWilliams how? im lost as I have tried double quotes and all sorts.. can you please provide an example. Jan 31, 2020 at 1:43
  • 2
    Have you tried just using TableDestination := myDestinationWorksheet.Range("A1") ? Jan 31, 2020 at 1:43

1 Answer 1

1

You can use ranges directly without constructing the "stringified" version of them:

Dim pc As PivotCache, pt As PivotTable
Dim wsSrc As Worksheet, wsDest As Worksheet
Dim rngSrc As Range, rngDest As Range

Set wsSrc = ActiveSheet
Set wsDest = ActiveSheet   'for convenience here

Set rngSrc = wsSrc.Range("C5:E14")  'source data
Set rngDest = wsDest.Range("I5")    'table location

Set pc = wsDest.Parent.PivotCaches.Create(SourceType:=xlDatabase, _
                        SourceData:=rngSrc, Version:=6)

Set pt = pc.CreatePivotTable(TableDestination:=rngDest, _
                      TableName:="PivotTable2", DefaultVersion:=6)
4
  • ...although the docs do recommend using a string for SourceData in PivotCaches.Create. Or is that fixed depending on the version of Excel?
    – BigBen
    Jan 31, 2020 at 2:03
  • can confirm the above works. I thought also needed a string as per MS Docs.. But alas the above is so neat and succinct Jan 31, 2020 at 2:40
  • learn.microsoft.com/en-us/office/vba/api/… here TableDestination is a Variant and the example code uses a Range object as the argument. Jan 31, 2020 at 2:53
  • From PivotCaches.Create: "When passing a Range object, we recommend that you either use a string to specify the workbook, worksheet, and cell range, or set up a named range and pass the name as a string. Passing a Range object may cause "type mismatch" errors unexpectedly." I'm wondering if this is still an issue? Definitely answered one or two questions with that error.
    – BigBen
    Jan 31, 2020 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.