SolemnSpirit
New Member
Hi all,
I have a workbook that has a data sheet and two sheets that each have a different pivot table on them.
The pivot tables are created using vba code and, after initially struggling to get the 2nd pivot table to appear, this is now working. However I want to have slicers attached to these pivots (each only affecting a single table) and while the code I have seems to be correct excel will only create one set of slicers and not the other. Originally it was creating only the 1st set of slicers and not the 2nd but now somehow it is creating the 2nd and not the 1st.
The forum will not allow me to copy the full code for both pivot/slicer sets into this post so I have attached a copy of the workbook. The code is in the modules Sub_Make_1st_Pivot and Sub_Make_2nd_Pivot. I have also copied the code for the 1st pivot/slicer set below. The 2nd set is basically the same code but with PivotTableName changed and PSheet changed to Sheet3.
I appreciate you taking the time to look at this and for any help/guidance you may have.
Many thanks,
Chris
1st Pivot/Slicer Set:
I have a workbook that has a data sheet and two sheets that each have a different pivot table on them.
The pivot tables are created using vba code and, after initially struggling to get the 2nd pivot table to appear, this is now working. However I want to have slicers attached to these pivots (each only affecting a single table) and while the code I have seems to be correct excel will only create one set of slicers and not the other. Originally it was creating only the 1st set of slicers and not the 2nd but now somehow it is creating the 2nd and not the 1st.
The forum will not allow me to copy the full code for both pivot/slicer sets into this post so I have attached a copy of the workbook. The code is in the modules Sub_Make_1st_Pivot and Sub_Make_2nd_Pivot. I have also copied the code for the 1st pivot/slicer set below. The 2nd set is basically the same code but with PivotTableName changed and PSheet changed to Sheet3.
I appreciate you taking the time to look at this and for any help/guidance you may have.
Many thanks,
Chris
1st Pivot/Slicer Set:
Code:
Sub MakePivot1()
' |-------- PIVOT TABLE 1 START --------|
ProgressForm "Reporting"
Dim PSheet As Worksheet, DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastSourceRow As Long, LastSourceCol As Long
On Error Resume Next
Set PSheet = Worksheets("Summary")
PSheet.Activate
Set DSheet = Worksheets("Data")
PivotTableName = "PivotTable1"
LastSourceRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastSourceCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastSourceRow, LastSourceCol)
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(3, 7), TableName:=PivotTableName)
ProgressForm "None", "Creating pivot table (" & PivotTableName & ")"
With PSheet
With .PivotTables(PivotTableName).PivotFields("Employee Name")
.Orientation = xlRowField
.Position = 1
.LayoutBlankLine = True
.Function = xlNone
End With
With .PivotTables(PivotTableName).PivotFields("Blank For Spacing")
.Orientation = xlRowField
.Position = 2
.LayoutBlankLine = True
.Function = xlNone
End With
With .PivotTables(PivotTableName).PivotFields("Charge Code (Full)")
.Orientation = xlRowField
.Position = 3
.LayoutBlankLine = True
.Function = xlNone
End With
With .PivotTables(PivotTableName).PivotFields("Panel No. (Full)")
.Orientation = xlRowField
.Position = 4
.LayoutBlankLine = True
.Function = xlNone
End With
With .PivotTables(PivotTableName).PivotFields("Total Time")
.Orientation = xlDataField
.Position = 1
.LayoutBlankLine = True
.Function = xlSum
.NumberFormat = "[h]:mm:ss"
.Name = "Sum of Total Time"
End With
.PivotTables(PivotTableName).ShowTableStyleRowStripes = False ' Turn off alternating colour rows
.PivotTables(PivotTableName).TableStyle2 = "NewPivotStyle" ' Set colour to custom style
.PivotTables(PivotTableName).SubtotalLocation xlAtBottom ' Set subtotals to appear at bottom of groups
.PivotTables(PivotTableName).PivotFields("Blank For Spacing").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
.PivotTables(PivotTableName).PivotFields("Charge Code (Full)").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
.PivotTables(PivotTableName).PivotFields("Panel No. (Full)").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
.PivotTables(PivotTableName).ShowDrillIndicators = False ' Turn off +/- buttons
.PivotTables(PivotTableName).DisplayFieldCaptions = False ' Turn off headers
.PivotTables(PivotTableName).HasAutoFormat = False ' Stop pivot from resizing on update
.Columns("G").ColumnWidth = 39.71
End With
' |-------- PIVOT TABLE 1 END --------|
End Sub