View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
ryguy7272 ryguy7272 is offline
external usenet poster
 
Posts: 2,836
Default Can I Sort (A-Z or Z-A) Pivot Table Data or Fields using VBA?

Seems like you are familiar with macro recording, but just to be sure...
1) Turn on the macro recorder
2) Double-click the column you want to sort by
3) Click advanced
4) AutoSort Options €“ Ascending/Descending
5) Click Ok, Click Ok
6) Turn off the macro recorder
7) Place the codes in the appropriate order of your sub€¦remember VBA is very
procedural, with the exception of IF...Then, calling Subs, etc.

Alternatively, with the macro recorder on, copy the Pivot Table, and
paste-special on a new sheet, then do the operations you want to do. That
may be an alternative to the option I mentioned above.

Good luck!
Ryan--

--
RyGuy


"MikeZz" wrote:

Hi,
I have some VBA that makes changes to multiple Pivot fields at once to show
various data in a Pivot Chart.

Is there a clean way to have VBA sort the Pivot by say the Grand Total
Column (either reverse or ascending) so that the pivot bar chart looks like a
pareto.... tallest bars at the left of the stacked bar chart?

As an example of a simple single column ("Customer") & row ("Group") field,
I want to sort the data so that the customer with highest sales is at the
top of the list.

Here's a recording of a macro but it just seems to reference cells in the
worksheet. The problem is that if I change the colum/row fields, the
cell/column/row of the total will also change.

Range("I7").Select
Selection.Sort Key1:="R7C9", Order1:=xlAscending, Type:=xlSortValues, _
OrderCustom:=1, Orientation:=xlTopToBottom
Selection.Sort Key1:="R7C9", Order1:=xlDescending, Type:=xlSortValues, _
OrderCustom:=1, Orientation:=xlTopToBottom
Range("A7").Select
Selection.Sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlTopToBottom
Range("B6").Select
Selection.Sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlLeftToRight
Selection.Sort Order1:=xlDescending, Type:=xlSortLabels, OrderCustom:=1, _
Orientation:=xlLeftToRight