sn152
Member
Hi All,
I am trying to create multiple pivot tables in one worksheet with the same data source. The code below is for one pivot table. I am not sure how to create the 2nd, 3rd, 4th, 5th and 6th pivot tables in the same worksheet.
Please help me.
Thanks in advance!
I am trying to create multiple pivot tables in one worksheet with the same data source. The code below is for one pivot table. I am not sure how to create the 2nd, 3rd, 4th, 5th and 6th pivot tables in the same worksheet.
Code:
Dim PSheet As Worksheet
Dim SSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LRow As Long
Dim LCol As Long
'Insert a new worksheet for Pivot table
On Error Resume Next
Application.DisplayAlerts = False
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set SSheet = Worksheets("Data")
'Define the source data range
LRow = SSheet.Cells(Rows.Count, 1).End(xlUp).Row
LCol = SSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = SSheet.Cells(1, 1).Resize(LRow, LCol)
'Create Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 1), _
TableName:="TicketsCreated")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(2, 1), TableName:="TicketsCreated")
'Insert Row Fields
With ActiveSheet.PivotTables("TicketsCreated").PivotFields("Updated by")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With ActiveSheet.PivotTables("TicketsCreated").PivotFields("Updated")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("TicketsCreated").PivotFields("Updated")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
End With
Please help me.
Thanks in advance!