Dynamic VBA Macro to Create Pivot Table

Bkisley

Board Regular
Joined
Jan 5, 2017
Messages
100
I have tried everything I know so far with no success.

Trying to do
I have a macro that creates a new tab and converts a data set into a formatted table named "Data Table"
The next macro (until I get this one working which I will then combine with my first macro) needs to create a new tab called "Pivot Table" and create a Pivot Table named "Pivot Table Final" of the formatted table named "Data Table" in cell A1. You will see the range I have is A1:R1000 this is just to be sure everything is captured.

This is where I keep getting stuck. It shows "invalid procedure call or argument". I have tried changed the table name directly in the code below and removing the last part of the code shown here, but still nothing.
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Date Table!R1C1:R1000C18", Version:=6).CreatePivotTable TableDestination:= _
"Pivot Table!R1C1", TableName:="PivotTable3", DefaultVersion:=6
ActiveSheet.PivotTables("PivotTable3").Name = "PivotTableFinal"

PLEASE HELP!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this (didn't check):
VBA Code:
Dim PTCache As PivotCache, PTable As PivotTable, psh As Worksheet

Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Worksheets("YourDataSheetName").Range("A1").CurrentRegion)
  
ActiveWorkbook.Sheets.Add Before:=Worksheets(1)
Set psh = Worksheets(1):    psh.Name = "PivotTable"
  
Set PTable = ActiveSheet.PivotTables.Add(PTCache, Range("A1"))
PTable.Name = "PivotTableFinal"
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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