VBA Pivot Table Error 5 - 0_o

corquando

Board Regular
Joined
Jan 8, 2009
Messages
82
Hello, MrExcel World.

Rich (BB code):
    Dim PRange As Range
    Dim lastRow As Long
    Dim finalCol As Long
 
 
 
            lastRow = ActiveSheet.Cells(Application.Rows.Count, 1).End(xlUp).Row
            finalCol = ActiveSheet.Cells(1, Application.Columns.Count).End(xlToLeft).Column
 
            Set PRange = Sheets("Final Rollup").Cells(1, 1).Resize(lastRow, finalCol)
 
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
               PRange, Version:=xlPivotTableVersion12).CreatePivotTable _
               TableDestination:="Final Rollup!R2C5", TableName:="CoreCounts", _
               DefaultVersion:=xlPivotTableVersion12

It stops at the red part and I get a "Run-time error '5': Invalid procedure call or argument."

Oddly, the code came from the macro recorder itself and threw the error even before I transplanted PRange. Everything else is unchanged.

I'm pretty sure I have all the references necessary . . . I am clueless.

Thanks for any help.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hello corquando,

One possible cause is that you are already using that PivotTable name in the same worksheet.

Excel typically creates new PivotTables using an unused name ie. PivotTable1, PivotTable2, etc.

If you record a macro while creating PivotTable2, then try to run that macro immediately after,
you'll get the error you describe because PivotTable2 already exists.
 
Upvote 0
Thanks, Jerry.

I'll review the code and process to see if this is the case. I usually try to stay aware of redundancies like that, but it's the ones buried in the heiroglyphics that get by me when they do.

Happy coding.
 
Upvote 0
You're welcome. Just to clarify, the problem wouldn't be buried in other parts of your VBA code.
It would be if you run this macro a second time without deleting the previously created PivotTable with the same name: "CoreCounts"
 
Upvote 0
If the worksheet's name includes spaces you need to surround it with single quotes:

Rich (BB code):
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
               PRange, Version:=xlPivotTableVersion12).CreatePivotTable _
               TableDestination:="'Final Rollup'!R2C5", TableName:="CoreCounts", _
               DefaultVersion:=xlPivotTableVersion12
 
Upvote 0
If the worksheet's name includes spaces you need to surround it with single quotes:

Rich (BB code):
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
               PRange, Version:=xlPivotTableVersion12).CreatePivotTable _
               TableDestination:="'Final Rollup'!R2C5", TableName:="CoreCounts", _
               DefaultVersion:=xlPivotTableVersion12

Ahh- good catch!
 
Upvote 0
@ Jerry - thanks, gotcha. I'll store that under "Dumb little things that mess up the code."

@Dr. Poulsom - *facepalm* it is the things like this that keep me humble and meek. I knew that - I write more VLOOKUP commands in R1C1 than the next 47 guys. Thanks for the reminder.

It runs beautifully now. Thanks again to both you guys.

Best. Site. Ever.
 
Upvote 0

Forum statistics

Threads
1,224,900
Messages
6,181,635
Members
453,059
Latest member
jkevin

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