Auto Sorting an Area

default_name

Board Regular
Joined
May 16, 2018
Messages
170
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
I have a group of cells (A2:C20) that constantly has new information.

I always have to go into the data/range and sort the values so that they are in order by date.
Since the content of this group of cells is always changing, it is kind of annoying to have to re-sort the list all the time.

Is there a way to only automate this area (A2:C20) of the spreadsheet, so that it automatically sorts by date (column A) as I enter data?

Here's an example:

A
BC
1Date
Item
Amount
24/25/19ABCCC11234$463.78
35/19/19JNLNFMSL2345$1173.12
45/21/19KNDFN13205$24.14
59/1/19DASFHK734N$338.25
6
7

<tbody>
</tbody>

If I enter in a value at the end, like this:

A
BC
1Date
Item
Amount
24/25/19ABCCC11234$463.78
35/19/19JNLNFMSL2345$1173.12
45/21/19KNDFN13205$24.14
59/1/19DASFHK734N$338.25
64/29/19FDBN2283KJK$6792.02
7

<tbody>
</tbody>

I'd like it to automatically re-sort itself like so:

A
BC
1Date
Item
Amount
24/25/19ABCCC11234$463.78
34/29/19FDBN2283KJK$6792.02
45/19/19JNLNFMSL2345$1173.12
55/21/19KNDFN13205$24.14
69/1/19DASFHK734N$338.25
7

<tbody>
</tbody>

Is this even possible?

Thanks in advance for your help
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim rw As Long
    
'   Exit sub if more than one cell updated at a time
    If Target.CountLarge > 1 Then Exit Sub
    
'   See where change occured
    Set rng = Intersect(Target, Range("A2:C20"))
'   If change occurred outside specified range, exit sub
    If rng Is Nothing Then Exit Sub
    
'   Get row that change happened in
    rw = Target.Row
    
'   See if all three columns populated in target row
    If Cells(rw, "A") <> "" And Cells(rw, "B") <> "" And Cells(rw, "C") <> "" Then
'       If so, sort range
        Range("A1:C20").Sort _
            key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
    End If

End Sub
As long as as you entering the data manually one row at a time, this will automatically sort the data once all three columns are populated.
 
Upvote 0
Hi default_name,

The easiest way is to add a worksheet_change event in the workbook which would trigger when any data is entered. I would suggest that it triggers once data is entered in the last column (column C in your case), and the code would look something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim n As Integer
Dim sortRng As Range

Application.ScreenUpdating = False

If Intersect(Target, Range("C:C")) Is Nothing Then
    '
Else
    n = Range("A" & Rows.Count).End(xlUp).Row
    Set sortRng = Range("A1:C" & n)


    sortRng.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
 
End If

Application.ScreenUpdating = True

End Sub

Add this code to the module of the worksheet where the sorting should happen. In order to adjust code, you can change "A1:C" range in case you have more than 3 columns, Range("C:C") in case that you want that event is triggered on any other cell, Order1 in case you want descending, Key1 in case you want that sorting is done according to different column.

Br
pella88
 
Last edited:
Upvote 0
Thanks guys! This is great!
Your comments in the code are very helpful as well!
Helps me better follow what is going on. :)
 
Upvote 0
You are welcome.
Glad we were able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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