Macro Button to sort when pressed

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
767
Office Version
  1. 365
Platform
  1. Windows
Good morning

I have written 2 pieces of code to align a column in my pivot table by highest / lowest values.
Instead of having 2 buttons (1 for each) is there anyway to adjust so each time the button is pressed it alternates between each value?
Is there a way to join both pieces of code into 1 button?

Code:
Private Sub CommandButton8_Click()
Range("N8").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Agent Name").AutoSort _
        xlAscending, "Avg Talk Time", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(3), 1
End Sub

Private Sub CommandButton9_Click()
Range("O8").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Agent Name").AutoSort _
        xlDescending, "Avg Work Time", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(5), 1
End Sub

thanks in advance
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I have tried to find out how to get the sort property of a range without much luck. That would be my go to solution if I could.
Otherwise you could have a module level variable (strSort) and one button. If strSort is null then sort ascending and set strSort to "Ascending". On 2nd click, test strSort and if not null, do the opposite sort of what the variable value is. The drawback to that is the data might get sorted and saved one way or the other. If that happens to be ascending then the code will appear to do nothing on the first click.
 
Upvote 0
You could use a toggle button and the type of sort depends on the state of the button. When the file loads, you could force it to run one or the other sorts and also force the toggle to the appropriate position. You can even change the text and color on the toggle to make it clear which sort has been run.

Radio buttons are another simple alternative.
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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