VBA Table Dynamically Sort Data and append Date Dynamically on new record Entered

PeeBee

New Member
Joined
Apr 17, 2016
Messages
19
Hi all ,
I have an excel Table dynamically Sorting this part works but the dynamically add datetime on record entry does not seem to work. If I add 2 rows of same itemcode e.g.: 5001 and delete top row of the 2 newly enterd records
then date appears on t remaining record.
I am no expert in the excel vba syntax how to put these 2 functions into 1 statement

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim SalesTable As ListObject
Dim SortCol As Range

Set SalesTable = ActiveSheet.ListObjects("Sales2022")
Set SortCol = Range("Sales2022[Code]")
'auto sort on enter
If Not Intersect(Target, SortCol) Is Nothing Then
    With SalesTable.Sort
      .SortFields.Clear
      .SortFields.Add Key:=SortCol, Order:=xlAscending
      .Header = xlYes
      .Apply
      

'Now auto add datetime in column5 on enter
 
 'If Not Intersect(Target, SortCol) Is Nothing Then
'insert date on data entered
      With Target(1, 5) ' column E add datetime on the fly
      .Value = Now
      .EntireColumn.AutoFit

End With
  '  End If
 End With
     End If
End Sub


any help appreciated

thank you Peter
 

Attachments

  • DateAppendError.jpg
    DateAppendError.jpg
    203.4 KB · Views: 6

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I think I have worked this out but there maybe a better way to write this?
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim SalesTable As ListObject
Dim SortCol As Range

Set SalesTable = ActiveSheet.ListObjects("Sales2022")
Set SortCol = Range("Sales2022[Code]")

'Auto Sort on enter
If Not Intersect(Target, SortCol) Is Nothing Then
    With SalesTable.Sort
      .SortFields.Clear
      .SortFields.Add Key:=SortCol, Order:=xlAscending
      .Header = xlYes
 'If Not Intersect(Target, SortCol) Is Nothing Then
'insert date on data entered
      With Target(1, 5) ' column E add datetime on the fly
      .Value = Now
     .EntireColumn.AutoFit
             
End With
   .Apply     ' I have shifted .apply down here ?

 End With
     End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

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