Automatically sort by date

shnickles

New Member
Joined
Nov 5, 2016
Messages
3
Hey guys, I was doing some searching and I came across a few threads discussing this topic already. However, I'm entirely new to VBA and I don't know how to edit some of the suggested codes to fit my particular data.

f12932326f.png


I just want everything sorted by date (Column D) without having to re-sort every time I add a new item.

Let me know if y'all need any other info to help. Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:
The range will sort when you change the value in column "E"
The range will be sorted by the values in column "D"

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
If Not Intersect(Target, Range("E3:E" & Lastrow)) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Range("C3:E" & Lastrow).Sort Key1:=Target.Offset(0, -1)
End If
End Sub
 
Upvote 0
The script runs when you change a value in column "E".

And it is assuming you only have 3 columns of data
If these assumptions are not true I need more details
 
Upvote 0
Try this:
The range will sort when you change the value in column "E"
The range will be sorted by the values in column "D"

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

Thanks for your reply! I actually have four columns of data; I set up a code in column B to add a checkmark upon double-click:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Intersect(Target, Range("B3:B50")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = ChrW(&H2713) Then
    Target.ClearContents
    Cancel = True
Else
    Target.Value = ChrW(&H2713)
    Cancel = True
End If
End If
    Application.EnableEvents = True
End Sub

When I added your code, it only works for columns C-E. I tried edited it to include column B, but then the data will only sort if there actually is a checkmark added. It won't sort if I add a new item that isn't marked done.
 
Upvote 0
Try this

Now It should work.
The sort portion activates when you enter a value in column "E"
Not sure what your using the check mark portion for but it works.
Let me know if this meets your needs
My change script allows you to keep going down the sheet forever
Your portion with the checkbox stops at row 50. Not sure if you really wanted that or did not know about Lastrow




Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
If Not Intersect(Target, Range("E3:E" & Lastrow)) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Range("B3:E" & Lastrow).Sort Key1:=Target.Offset(0, -1)
End If
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
If Not Intersect(Target, Range("B3:B50")) Is Nothing Then
Application.EnableEvents = False
If Target.Value = ChrW(&H2713) Then
    Target.ClearContents
    Cancel = True
Else
    Target.Value = ChrW(&H2713)
    Cancel = True
End If
End If
    Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Try this

Now It should work.
The sort portion activates when you enter a value in column "E"
Not sure what your using the check mark portion for but it works.
Let me know if this meets your needs
My change script allows you to keep going down the sheet forever
Your portion with the checkbox stops at row 50. Not sure if you really wanted that or did not know about Lastrow

Thanks so much! This worked perfectly.

(I'm just using the checkmarks to mark off items in a to-do list - I actually copied the code from another site and didn't know about Lastrow at all. Thanks!)
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
Thanks so much! This worked perfectly.

(I'm just using the checkmarks to mark off items in a to-do list - I actually copied the code from another site and didn't know about Lastrow at all. Thanks!)
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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