creating multiple pivot tables

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Creating multiple pivot tables on one sheet | Excel forum

Creating multiple pivot tables on one sheet | Excel forum

resolvedResolved · High Priority · Version 2007

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

Thanks for the reply

That worked perfectly

Sam

Edited on Fri 30 Nov 2012, 10:50

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.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Autosum shortcut key

press ALT + =

View all Excel hints and tips


Server loaded in 0.08 secs.