How to auto sort date when date is entered or changed in Excel?
In Excel, the Sort function can help you to sort date in ascending or descending order as you need. But it isn’t dynamic, if you have sorted the date and then add new date to it, you would need to sort it again. Are there any good and quick ways for you to auto sort the date when entering new date each time in a worksheet?
For example, the original date in Column A, the following formula can help you to auto sort the date or any other text strings in a new helper column based on the column that you want to sort, please do as follows:
1. Enter this formula:
=INDEX($A$2:$A$15,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$15,"<="&$A$2:$A$15),0)) into a blank cell beside your date column, C2, for example, and then press Ctrl + Shift + Enter keys together, and you will get a number sequence, then drag the fill handle down to the cells that you want to use, see screenshot:
Note: In above formula: A2:A15 is your original date range that you want to auto sort.
2. Then format the numbers as date format by clicking Short Date from the General drop down list under the Home tab, see screenshot:
3. Then the sequence numbers have been converted to date format, and the original date has been sorted as well, see screenshot:
4. From now on, when you entering new date or change the date in column A, the date in column C will be sort in ascending order automatically, see screenshot:
The following VBA code can help you to auto sort the date in the original column when you enter new date or change the date as you need.
1. Go the worksheet that you want to auto sort the date when you enter or change a date.
2. Right click the sheet tab, and select View Code from the context menu, in the popped out Microsoft Visual Basic for Applications window, please copy and paste the following code into the blank Module window, see screenshot:
VBA code: auto sort when date is entered or changed:
Private Sub Worksheet_Change(ByVal Target As Range) 'Updateby Extendoffice On Error Resume Next If Application.Intersect(Target, Application.Columns(1)) Is Nothing Then Exit Sub If Target.Count > 1 Then Exit Sub Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom End Sub
Note: In the above code, the entered date will be auto sort in column A, you can change A1 and A2 to your own cells as you need.
3. From now on, when you enter date in column A, the date will be sorted ascending automatically.