Macro to Create Pivot Table on Existing WorkSheet

L

Legacy 367316

Guest
Trying to created a Pivot Table on an existing worksheet. This is the code that was recorded during the record macro. I stopped recording and then ran the code but when I go to run it again it gives me an error. Run-time error '5': invalid procedure call or argument
Any help would be greatly appreciated.


Sub Macro12()
'
' Macro12 Macro
'

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Outage Data!R1C1:R526C9", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="CEMI Devices!R3C5", TableName:="PivotTable4", _
DefaultVersion:=xlPivotTableVersion14




End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hazard a guess at "PivotTable4" already existing, if you want to recreate the pivot table you'll need to delete it first.
 
Upvote 0
Trying to created a Pivot Table on an existing worksheet. This is the code that was recorded during the record macro. I stopped recording and then ran the code but when I go to run it again it gives me an error. Run-time error '5': invalid procedure call or argument
Any help would be greatly appreciated.


Sub Macro12()
'
' Macro12 Macro
'

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Outage Data!R1C1:R526C9", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="CEMI Devices!R3C5", TableName:="PivotTable4", _
DefaultVersion:=xlPivotTableVersion14




End Sub
The problem is within the SourceData, excel may not give the proper code even when it's being recorded.
What I did was declare the pivot table as variable:
Code:
Dim Pt As PivotTable
Sheets("desired_Sheet").Select
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Sheets("desired_Sheet").Range("A1").CurrentRegion, _
    Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="pivot_table!R1C1", TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion14)
-probably selecting the sheet isn't necessary-

You may do many thing with the Pt being declared as variable with the command "With PT -or however you named your pivottable variable- "
 
Upvote 0
I changed the code a bit with your guys suggestions but still no luck. I corrected the source data and the pivot table name. I believe the issue is with the table destination, I want the pivot table in sheet CEMI Devices and in cell E3

Sub Macro12()
'
' Macro12 Macro
'

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Event Table!R1C1:R82C15", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="CEMI Devices!R3C5", TableName:="PivotTable99", _
DefaultVersion:=xlPivotTableVersion14




End Sub
 
Upvote 0
Also tried with no luck

Sub Macro12()
'
' Macro12 Macro
'

Dim Pt As PivotTable
Sheets("CEMI Devices").Select
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("Event Table").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="CEMI Devices!R1C1", TableName:="PivotTable1001", _
DefaultVersion:=xlPivotTableVersion14)




End Sub
 
Upvote 0
Also tried with no luck

Sub Macro12()
'
' Macro12 Macro
'

Dim Pt As PivotTable
Sheets("CEMI Devices").Select
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("Event Table").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="CEMI Devices!R1C1", TableName:="PivotTable1001", _
DefaultVersion:=xlPivotTableVersion14)




End Sub
I remember why I don't use spaces in sheet names, that seems to be the problem, try to change "Event Table" to Event_Table and "CEMI Devices" to "CEMI_Devices"
And yeah, you don't have to select the sheet.
Code:
Sub CreatePivotTable()
[COLOR=#333333]Dim Pt As PivotTable[/COLOR]
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Sheets("Event_Table").Range("A1:Z100"), _
    Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="CEMI_Devices!R1C1", TableName:="PivotTable2", _
    DefaultVersion:=xlPivotTableVersion14)
End Sub
Change the Range as you wish for both pivot table and in the sheets Event_Table
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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