I have a macro to create a pivot table that works great except when I try to sort the Columns. I have a column called "Aging Category" with 0-7 days, 8-14 days, 15-30 days, 31-90 days, 91-180 days, and 181+ days. Not all categories will always be represented in the raw data, but want to include as a possibility. here is the code I'm using to try and sort:
If I do not include "On Error Resume Next" it fails if one of the categories is not found. Is there another method of sorting I can use?
Here is the full code:
Code:
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"181+ Days").Position = 1
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"91-180 Days").Position = 2
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"31-90 Days").Position = 3
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"15-30 Days").Position = 4
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"8-14 Days").Position = 5
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"0-7 Days").Position = 6
If I do not include "On Error Resume Next" it fails if one of the categories is not found. Is there another method of sorting I can use?
Here is the full code:
Code:
Sub CreateINCPivot()
On Error Resume Next
Sheets("INCData").Select
ColCount = ActiveSheet.UsedRange.Columns.Count
RowCount = ActiveSheet.UsedRange.Rows.Count
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"INCData!R1C1:R" & RowCount & "C" & ColCount, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Aging!R5C1", TableName:="AgingINCpivot", DefaultVersion _
:=xlPivotTableVersion12
Sheets("Aging").Select
Cells(5, 1).Select
ActiveSheet.PivotTables("AgingINCpivot").AddDataField ActiveSheet.PivotTables( _
"AgingINCpivot").PivotFields("Number"), "Count of Number", xlCount
With ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Assignment Group")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Assigned To")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Number")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category")
.Orientation = xlColumnField
.Position = 1
End With
Range("A5").Select
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Assignment Group").ShowDetail _
= False
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Assigned To").ShowDetail _
= False
ActiveSheet.PivotTables("AgingINCpivot").DataPivotField.PivotItems("Count of number") _
.Caption = "Count"
ActiveSheet.PivotTables("AgingINCpivot").CompactLayoutColumnHeader = _
"Days Open"
ActiveSheet.PivotTables("AgingINCpivot").CompactLayoutRowHeader = "Workgroup"
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"181+ Days").Position = 1
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"91-180 Days").Position = 2
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"31-90 Days").Position = 3
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"15-30 Days").Position = 4
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"8-14 Days").Position = 5
ActiveSheet.PivotTables("AgingINCpivot").PivotFields("Aging Category").PivotItems( _
"0-7 Days").Position = 6
End Sub