Code to sort pivot table by 'Grand Total'

muaythai

Board Regular
Joined
Aug 24, 2004
Messages
60
Version: Excel 03 SP1.

I'm using the following code to try and automate a pivot table refresh by descending on the grand total.

The data contained in the table is just a count of occurrences but whenever I use this it comes up with :-

'Can't identify which pivot field to sort by'. Error

Any assistance would be much appreciated.

Sub SortGrandTotal()
Columns("B:IV").Select
Selection.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Select
ActiveCell.Offset(1, 0).Activate
MyCell1 = ActiveCell.Address
MyCol = ActiveCell.Column
MyCell2 = Cells(92, MyCol).Address
Range(MyCell1, MyCell2).Select
Selection.Sort Key1:="b5", Order1:=xlDescending, Type:=xlSortValues, OrderCustom:=1, Orientation:=xlTopToBottom

End Sub


MT
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Cannot see what you are trying to do.

If you want the total column sorted, doubleclick the grey header, then Advanced, and the option is there.
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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