Switch between Ascending and Descending order by one macro [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi,

I use this macro (see below) to sort the data in ascending order in the table.

My macro is assigned to the cutom MSO button.

I'd like to transfer my macro to the state which sorts the data in ascending order on the first click, sorts the data in descending order on the second click and again in ascending order on the third click, and so on.

Basically, I want to switch between ascending and descending order by using one macro (one button).

Thank you. Much appreciated.

VBA Code:
Public Sub Ascending_Order(ByRef control As Office.IRibbonControl)

With Application
.ScreenUpdating = False

Dim LastCell As Range

With ActiveSheet
Set LastCell = .Cells(Rows.Count, 2).End(xlUp)
.Range("D3", LastCell).sort Key1:=.Range("D3"), Order1:=xlAscending, Header:=xlYes, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
End With

With Range("B3").Font
.Color = vbGreen: .Bold = True
End With

With Range("U3,N3,O3,P3,Q3.F3").Font
.Color = vbWhite: .Bold = False
End With

.ScreenUpdating = True
End With

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
VBA Code:
Public Sub Ascending_Order(ByRef control As Office.IRibbonControl)
    Static x As Boolean '// For swapping state
    Dim sort_order As XlSortOrder
 
    With Application
        .ScreenUpdating = False
     
        Dim LastCell As Range
     
        With ActiveSheet
            Set LastCell = .Cells(Rows.Count, 2).End(xlUp)
            sort_order = IIf(x, xlDescending, xlAscending) '//Select order
            x = Not x                                      '//Swap state
            .Range("D3", LastCell).Sort Key1:=.Range("D3"), Order1:=sort_order , Header:=xlYes, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
        End With
     
        With Range("B3").Font
            .Color = vbGreen: .Bold = True
        End With
     
        With Range("U3,N3,O3,P3,Q3.F3").Font
            .Color = vbWhite: .Bold = False
        End With
     
        .ScreenUpdating = True
    End With

End Sub
 
Upvote 0
Solution
Maybe something like this:
VBA Code:
Dim flag As Boolean

Sub to_sort()
Dim tx As Variant

    flag = Not flag
    If flag Then tx = xlAscending Else tx = xlDescending
    Range("A1:A10").Sort Key1:=Range("A1"), Order1:=tx, Header:=xlYes

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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