VBA Code to sort multiple columns

KMH

New Member
Joined
Nov 10, 2022
Messages
23
Office Version
  1. 2013
Platform
  1. Windows
I am creating a To Do List and want to automatically sort by Due Date (Column B), then Energy / Focus Required (Column C) and then Task (Column A)

What code can I use that would automatically do this once data is entered in Column B.

TASKDUE DATEENERGY / FOCUS REQUIREDCOMPLETED?
SEND DATASETS TO DASHBOARD CREATOR12-Jul-23LOW ENERGY / HIGH FOCUS
FINALIZE STAFF MEETING SLIDES AND HANDOUTS13-Jul-23LOW ENERGY / LOW FOCUS
RELIST DEFERRED APPLICATIONS FROM 29-JUN-202313-Jul-23LOW ENERGY / LOW FOCUS
CLEAR FLAGGED EMAILS14-Jul-23LOW ENERGY / HIGH FOCUS
COMPLETE FINAL CHANGES TO WP FORMS AND SEND DRAFT TO SIAN AND SASKIA14-Jul-23HIGH ENERGY / HIGH FOCUS
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Change it to sort each column as ascending or descending to suit...

VBA Code:
Sub Sorter()
    
    Columns("A:D").Sort Key1:=Range("B2"), Order1:=xlAscending, _
                        Key2:=Range("C2"), Order2:=xlAscending, _
                        Key3:=Range("A2"), Order3:=xlAscending, _
                        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                        DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
End Sub
 
Upvote 0
Thanks. The code works but it's not sorting it automatically once I enter the date. Is there something else I have to do?
 
Upvote 0
To install the code...
  • Right-click on the sheet tab
  • Select View Code from the pop-up context menu
  • Paste the code below in the Worksheet code module

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And Target(1).Column = 2 And Target(1).Value <> Empty Then
            Columns("A:D").Sort Key1:=Range("B2"), Order1:=xlAscending, _
                                Key2:=Range("C2"), Order2:=xlAscending, _
                                Key3:=Range("A2"), Order3:=xlAscending, _
                                Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
    End If
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,216,759
Messages
6,132,551
Members
449,735
Latest member
Gary_M

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