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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,348
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,697
Messages
5,855,226
Members
431,712
Latest member
Stella J

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