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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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
 
Upvote 0

Forum statistics

Threads
1,211,839
Messages
6,104,292
Members
447,901
Latest member
boy3hc2004

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