Auto sort after date stamp

HDavidson

New Member
Joined
Sep 23, 2018
Messages
17
I am using the Vba code below to apply a date stamp in a spreadsheet .How can i adapt this to auto sort all the data with newest entry at the top.The spreadsheet covers the columns A to J inclusive


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 Then Cells(Target.Row, "j") = Now()
End Sub


Thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hello,

You can insert the following

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Count > 1 Then Exit Sub
   Dim last As Long
   last = ActiveSheet.Cells(Application.Rows.Count, "A").End(xlUp).Row
   Application.ScreenUpdating = False
   If Target.Row > 1 Then Cells(Target.Row,"J") = Now()
   Range("A1:J" & last).Sort [J1], xlAscending, Header:=xlYes
   Range("J1").Select
   Application.ScreenUpdating = True
End Sub

Hope this will help
 
Last edited:
Upvote 0
Thanks james that returns the runtime error below

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Count > 1 Then Exit Sub
   Dim last As Long
   last = ActiveSheet.Cells(Application.Rows.Count, "A").End(xlUp).Row
   Application.ScreenUpdating = False
   If Target.Row > 1 Then Cells(Target.Row, "J") = Now()
   Range("A1:J" & last).Sort [J1], xlAscending, Header:=xlYes Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Count > 1 Then Exit Sub
   Dim last As Long
   last = ActiveSheet.Cells(Application.Rows.Count, "A").End(xlUp).Row
   Application.ScreenUpdating = False
   If Target.Row > 1 Then Cells(Target.Row, "J") = Now()
   Range("A1:J" & last).Sort [J1], xlAscending, Header:=xlYes
   Range("J1").Select
   Application.ScreenUpdating = True
End Sub

Thanks for looking 
   Range("J1").Select
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for looking james i get a runtime error when this is run .The debug indicates the error is in line 7

Code:
Range("A1:J" & last).Sort [J1], xlAscending, Header:=xlYes

Thanks again
 
Upvote 0
Re,

The spreadsheet covers the columns A to J inclusive

Does it mean you have headers in Row 1 ...and you data starting in Row 2 ...???
 
Upvote 0
Hello,

Can you make sure within the module of your Sheet ... to delete the previous version and replace it with exactly the following

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Count > 1 Then Exit Sub
   Dim last As Long
   last = ActiveSheet.Cells(Application.Rows.Count, "A").End(xlUp).Row
   Application.ScreenUpdating = False
   Application.EnableEvents = False
   If Target.Row > 1 Then Cells(Target.Row, "J") = Now()
   Application.ScreenUpdating = True
   Range("A1:J" & last).Sort [J1], xlAscending, Header:=xlYes
   Application.EnableEvents = True
End Sub

To be on the safe side ... just tested this event macro ...
and it does work fine ...

Hope this will help
 
Upvote 0
Hi James the now function wont change the time if i enter data into column 2 and wait 1 min and change it again so the last row edited does not move to the top of the list. Sorry to be a pain.


Thanks
 
Upvote 0
Glad you could fix your problem ...

Believe the issue was related to Ascending vs Descending ... :wink:
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,989
Members
452,541
Latest member
haasro02

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