Automatic Sorting as you Enter Dates Using VBA In Microsoft Excel 2010

In this article, you will learn how to automatically sort dates as you enter in the worksheet.

Q) I have a spreadsheet that's getting updated with new date entries every day. I'd like to sort the rows by newest date (date is in column C), and anytime I add a new date at the bottom I'd like it to automatically sort itself into the list after I enter it. Is there an easy way to do this?

 

Let us take an example:

Following is a picture of the data we have
 
img1
 
Following is the snapshot of the result we would like to see as we enter information.
 
img2
 
Click on Developer tab

From Code group, select Visual Basic
 
img3
 
Enter the following code in the sheet you are working. (sheet1 in our example)

Private Sub Worksheet_Change(ByVal Target As Range)

    On Error Resume Next

Range("A1").Sort Key1:=Range("C2"), _

      Order1:=xlAscending, Header:=xlYes, _

OrderCustom:=1, MatchCase:=False, _

      Orientation:=xlTopToBottom

End Sub
 
img4
 
In this way, you can easily sort the data by Dates column after entering the information. This will save our time in doing manual steps for sorting the data.
 
 

Comments

  1. How would I be able to do this exact function but on a new tab rather than the tab that has the database I do not want to sort my database by date.

  2. I am new at code and need help on automatically sorting from [row 20 down to row 590, columns A:L] by dates (older to newer) that i enter in column A20:A590 after entering whatever data i choose to enter in that row.
    Can someone please help me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.