toggle sort columns

J

JSnow

I have a button linked to a macro that will sort a data ascending. Is there
a way to click said button a second time and it will sord descending? Here's
the code thus far:

Sub sort_date()
Range("A3:T23").Sort Key1:=Range("G3"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
 
M

Mike H

Hi,

This test g3 and G4 to see which way they are sorted and then does the cort
in the opposite direction.

Sub sort_date()
If Left(Range("G3"), 1) > Left(Range("G4"), 1) Then
MyWay = xlAscending
Else
MyWay = xlDescending
End If
Range("A3:T23").Sort Key1:=Range("G3"), _
Order1:=MyWay, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

Mike
 
L

Lars-Åke Aspelin

If G3 and G4 are equal this macro will not work.
Better to compare G3 to G23, I think.

/ Lars-Åke
 
M

Mike H

Ah,

I never thought of that, good idea.

Mike

Lars-Ã…ke Aspelin said:
If G3 and G4 are equal this macro will not work.
Better to compare G3 to G23, I think.

/ Lars-Ã…ke
 
J

JSnow

Mike H, although I follow what's supposed to happen with this code, it
doesn't seem to work. The range, A4 (not A3, my bad) through T23 will sort
via row G4 but will only do so ascending. It doesn't switch to descending.
 
M

Mike H

Hi,

Modified and including the suggestion made by Lars-Ã…ke


Sub sort_date()
If Left(Range("G4"), 1) > Left(Range("G23"), 1) Then
MyWay = xlAscending
Else
MyWay = xlDescending
End If
Range("A4:T23").Sort Key1:=Range("G3"), _
Order1:=MyWay, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top