Conditional sort - ascending if sorted descending

Magriza

Well-known Member
Joined
Nov 16, 2005
Messages
508
Hi Guys and Gals,

A quick question if I may:

Is it possible to have a macro that runs from one button to sort a range in ascending order AND descending order? Obviously not at the same time.

So: if column A is sorted ascendingly and then the button is pressed again, the macro would check the range - if it's sorted ascendingly already it will then reverse the order.

Many thanks for all the help!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,792
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
one suggestion would be to have a sentry value in another cell outwith the sort range, i would suggest you put the value 1 in the cell.

Create your button and macro to do the sort, add an IF statement around the sort code on the lines of

IF range("B1").value = 1 then
sort ascending
else
sort descending
end if
range("B1").value = range("B1").value * -1

this would change the sentry value between 1 and -1
 

Magriza

Well-known Member
Joined
Nov 16, 2005
Messages
508
Hi jimrward,

Many thanks for your suggestion! I've incorporated it into my design and it works very well!

It also allows me to change my header based on the value assigned (1, 2 or nothing) to Heading ▲ or for ascending or descending using a standard if formula.

Sometimes I get too lost in the code that I forget you can use code in conjunction with cells! Thanks again for steering me to the right path.
 

Forum statistics

Threads
1,141,757
Messages
5,708,340
Members
421,566
Latest member
7Nabisco

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
Top