Sam has attended:
Excel VBA Intro Intermediate course
Creating multiple pivot tables on one sheet
Hi,
I am trying to create two pivot tables on one worksheet from the same data source. When i create the second pivot table, changing the destination and table name, the first pivot table is just moved to the new location.
many thanks
Sam
my code is
Sub MakePivots()
Dim DataRange As Range
Dim Destination As Range
' set data range for pivot tables
Set DataRange = Worksheets("Report").Range("a1:bo41")
'set destination for 1st pivot table
Set Destination = Worksheets("numbers").Range("A11")
'create 1st pivot table
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
SourceData:=DataRange, TableDestination:=Destination, TableName:="PagesByDay"
'add row and data fields
With Sheets("numbers").PivotTables("PagesByDay")
.PivotFields("Page Created Date").Orientation = xlRowField
.PivotFields("Fundraiser User Id").Orientation = xlDataField
End With
'set destination for second pivot table
Set Destination = Worksheets("numbers").Range("g11")
'create second pivot table - destination and name are differnet to 1st pivot table
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
SourceData:=DataRange, TableDestination:=Destination, TableName:="PagesByWeek"
'add row and data fields for 2nd pivot table
With Sheets("numbers").PivotTables("PagesByWeek")
.PivotFields("Page Created Date").Orientation = xlRowField
.PivotFields("Fundraiser User Id").Orientation = xlDataField
End With
End Sub
RE: Creating multiple pivot tables on one sheet
Hi Sam
Great idea have a macro create side by side pivot tables!
I've recreated your example and there seems to be just one line missing.
Add
Worksheets("Report").Select
before the line
ActiveSheet.PivotTableWizard ...
for both the 1st and 2nd pivot tables.
Also the macro will only run once. You could add the following 2 lines to clear the the pivot tables at the start after your Dim statements if any exist in the numbers sheet.
Worksheets("numbers").Select
Cells.Delete
Please let me know if that does the trick.
Regards
Doug
RE: Creating multiple pivot tables on one sheet
Hi Sam
Ignore this - Doug and I replied at the same time!
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Will be marked as resolved in 5 days
Notice: This is an automated message. Due to inactivity, this forum post will be marked as 'resolved' if there are no further responses in the next 5 days.