Merk
Merk

Reputation: 33

Sorting Pivot tables by Total in VBA

I have a VBA Macro that pulls information from a pivot table, but first I want to sort the Pivot table rows by the Grand Total. I had a Macro that worked, but the pivot table is not always the same length and I discovered that it will not work if the macro lists more columns than the pivot table contains. But it will also not work properly if the number of columns is not accurate. Does anyone know a good way to do this.

ActiveSheet.PivotTables("PivotTable1").PivotFields("Material").AutoSort _
        xlDescending, "Sum of QTY", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(60), 1

The 60 is preventing it from sorting a pivot table with only 59 columns, but a number like 58 in that place will not consider all the data.

I need it to be able to sort pivot columns of various sizes.

Upvotes: 0

Views: 1903

Answers (1)

Merk
Merk

Reputation: 33

I was able to solve it by just eliminating that restriction

ActiveSheet.PivotTables("PivotTable1").PivotFields("Material").AutoSort _
        xlDescending, "Sum of QTY"

It didn't affect the running of the macro at all to just eliinate it.

Upvotes: 1

Related Questions