Create Pivot Table using Macro

Hassesbar

New Member
Joined
Jan 15, 2004
Messages
7
Hi,
The Macro below works for a specific data source....how do I change it to work for any worksheet that I'm in when I run the macro, i.e creates a pivot table with all data in that sheet. The headings will always be called the same name

Thanks

Sub SchedulePivot()
'
' SchedulePivot Macro
' Macro recorded 15/01/2004 by ChristoferBeckman
'

'
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'sched export 15.1.04 (2)'!R1C1:R232C9").CreatePivotTable TableDestination:= _
"", TableName:="PivotTable1"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="pub_date", _
ColumnFields:="class_desc"
ActiveSheet.PivotTables("PivotTable1").PivotFields("M+S").Orientation = _
xlDataField
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,345
try this (not tested)

Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
[a1].CurrentRegion.Address).CreatePivotTable TableDestination:= _
"", TableName:="PivotTable1"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="pub_date", _
ColumnFields:="class_desc"
ActiveSheet.PivotTables("PivotTable1").PivotFields("M+S").Orientation = _
xlDataField
 

Watch MrExcel Video

Forum statistics

Threads
1,130,167
Messages
5,640,539
Members
417,151
Latest member
ChickenTenderer

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
Top