Arrange Pivot Table columns with VBA

shoki

New Member
Joined
Feb 9, 2011
Messages
3
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:

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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
when I know a category may not always appear I always add the main value, and use some sort of marker which I can then exclude, so for instance taking 0-7 which may not exist but in the data I have accommodated for that and ensure I add say an x for a value position I can exclude the x, yet it should always work
 
Upvote 0

Forum statistics

Threads
1,216,174
Messages
6,129,296
Members
449,498
Latest member
Lee_ray

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top