Run-time error '5' when creating a new pivot table

jgscanlon

New Member
Joined
Jan 8, 2014
Messages
46
I'm getting a "Run-time error '5': Invalid procedure call or argument" when running my code (below). I cannot figure out what I'm doing wrong here. Does anyone have any suggestions?

Code:
    Dim APXPvtSrcData As String
    Dim APXPvtCache As PivotCache
    Dim StartPvt As String
    Dim APXDataRowCount As String
    Dim APXPvtTable As PivotTable
    Dim i As Integer
           
    'Set pivot data range
    Sheets("APX Data").Activate
    Call CountRows(RowCount)
    APXDataRowCount = RowCount + 2
    
    APXPvtSrcData = Sheets("APX Data").Name & "!" & Range("A7:K" & APXDataRowCount).Address(ReferenceStyle:=xlA1)
    
    'Set Pivot Table location
    StartPvt = Sheets("APX Summary").Range("A3").Address(ReferenceStyle:=xlA1)
    
    'Create Pivot Cashe from Source Data
    Set APXPvtCache = Workbooks(OutputFile).PivotCaches.Create(xlDatabase, APXPvtSrcData)
    
    'Create Pivot Table from Pivot Cache
    Set APXPvtTable = APXPvtCache.CreatePivotTable(StartPvt, "PivotTable1")
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Pivot tables always give error when some sheet involved has spaces among, try to change them for "_"
IE:
Sheets("APX Data") for Sheets("APX_Data") -of course in your book as well-
 
Upvote 0
Let take this one piece at a time:

1) Your source must be a Range object, not a String
2) Your destination must be a Range object, not a String
3) Does your source range include the header row? It must, otherwise the CreatePivotCache will error
4) you already know the Sheet, so why the long syntax??
5) sheet names with spaces work just fine, but you need to surround them with single quotes when creating a cell/range reference string
6) unless you/your user has changed something, the default reference style is A1
7) Missing PivotCache parameter in PivotTables.Add() method

Part I
Your source must be a Range object, not a String ... Also, be sure the range includes the header row

Your Code...
Code:
    Dim APXPvtSrcData As String
    APXPvtSrcData = Sheets("APX Data").Name & "!" & Range("A7:K" & APXDataRowCount).Address(ReferenceStyle:=xlA1)

Correct and simplify...
Code:
    ' Define the source Range object
    Dim APXPvtSrcData As Excel.Range
    Set APXPvtSrcData = Sheets("APX Data").Range("A7:K" & APXDataRowCount)

Part II
This line should be fine, with the correction above to a Range object for the source

Your Code...
Code:
   'Create the Cache ... 
   Set APXPvtCache = Workbooks(OutputFile).PivotCaches.Create(SourceType:=xlDatabase, SourceData:=APXPvtSrcData )

Part III
Your destination must also be a Range object, not a String

Your Code...
Code:
    'Set Pivot Table location
    Dim StartPvt As String
    StartPvt = Sheets("APX Summary").Range("A3").Address(ReferenceStyle:=xlA1)

Correct and simplify...
Code:
    Dim StartPvt As Excel.Range
    Set StartPvt = Workbooks(OutputFile).WorkSheets("APX Summary").Range("A3")

Part IV
You are missing the PivotCache parameter

Your Code...
Code:
    'Create Pivot Table from Pivot Cache
    Set APXPvtTable = APXPvtCache.CreatePivotTable(StartPvt, "PivotTable1")

Correct and name your parameters (for clarity, and so the order does not matter)
Code:
   Set xlPivotTable = xlSheetDest.PivotTables.Add(PivotCache:=APXPvtCache , TableDestination:=APXPvtTable, TableName:="PivotTable1")

Make these fixes and you should be on your way.
Hope this helps,
Art
 
Upvote 0
Correction on the very last block, you add a pivot table to a sheet and I forgot to replace my variable names with yours :)

Corrected code...
Code:
Set APXPvtTable= Workbooks(OutputFile).WorkSheets("APX Summary").PivotTables.Add(PivotCache:=APXPvtCache , TableDestination:=APXPvtTable, TableName:="PivotTable1")

Art
 
Upvote 0
WOW!

Thank you so very much for your in-depth analysis and explanation! With your recommended changes, this pivot table is now being created just where I want it.

Thank you! :)
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top