I am creating a macro to generate some clustered column pivot charts. I have used examples from the internet/stack overflow to generate the pivot table, generate the pivot chart and display the columns in decreasing order.
I have consulted the Microsoft Documentation and tried fiddling with the syntax of PivotField.Autosort
.
Here is a generalised version of my code with comments. The Autosort command to display the columns in descending order is the last two lines:
'Declare Variables
Dim PSheet As Worksheet 'To create a sheet for a new pivot table.
Dim DSheet As Worksheet 'To use as a data sheet.
Dim PCache As PivotCache 'To use as a name for pivot table cache.
Dim PTable As PivotTable 'To use as a name for our pivot table.
Dim PRange As Range 'to define source data range.
Dim LastRow As Long 'To get the last row and column of our data range.
Dim LastCol As Long '
Dim chtObj As ChartObject
Dim PvtSht As Worksheet
Dim PvtTbl As PivotTable
'After inserting a new worksheet, this code will set the value of FSheet
'variable to pivot table worksheet and DSheet to source data worksheet.
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("MaintActivType").Delete
Worksheets("Sheet1").Select
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "MaintActivType"
Application.DisplayAlerts = True
Set PSheet = Worksheets("MaintActivType")
Set DSheet = Worksheets("Sheet1")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
'In Excel 2000 and above, before creating a pivot
'table you need to create a pivot cache to define the data source.
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="MaintenanceData")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="MaintenanceData")
'Insert Row Fields
With ActiveSheet.PivotTables("MaintenanceData").PivotFields("MaintActivType")
.Orientation = xlRowField
.Position = 1 'this field allows for sub-categories of data to be displayed
End With
'Insert Column Fields
With ActiveSheet.PivotTables("MaintenanceData").PivotFields("MaintActivType")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.Name = "Count of MaintActivType"
End With
' set the Pivot sheet
Set PvtSht = Sheets("MaintActivType")
' set the Pivot Table object
Set PvtTbl = PvtSht.PivotTables("MaintenanceData")
' set the Chart Object
Set chtObj = PvtSht.ChartObjects.Add(300, 200, 550, 200)
' modify ChartObject properties
With chtObj
.Chart.SetSourceData PvtTbl.TableRange2 ' set the chart's data range to the Pivot-Table's TableRange2
.Chart.ChartType = xlColumnClustered
.Name = "Maintenance Activity Type"
.Chart.HasTitle = True
.Chart.ChartTitle.Text = "Maintenance Activity Type"
.Chart.HasLegend = False
End With
ActiveSheet.PivotTables("MaintenanceData").PivotField("MaintActivType") _
.AutoSort xlDescending, "Sum of MaintActivType"
'End With
I don't get any error messages associated with the Autosort command, but if I comment out the On Error Resume Next
I get a Type mismatch error for the pivot cache creation ("Set Pcache ..."), which is baffling because that part works just fine with the error message suppressed.