Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Sheets(1).Range("A1") = ReturnWhenModifiedDate
End Sub
Option Explicit
Function ReturnWhenModifiedDate() As Date
'Called by Workbook Before_Save event
Dim fso As Object, f As Object
If thisworkbook.Path = vbNullString Then
'File has never been saved
ReturnWhenModifiedDate = Now() 'Return current date/time
Else
'File saved previously return that time
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(thisworkbook.Path & "\" & thisworkbook.Name)
ReturnWhenModifiedDate = f.DateLastModified
Set f = Nothing
Set fso = Nothing
End If
End Function
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Sheets("Second Trial").Range("A1") = ReturnWhenModifiedDate
End Sub
In the first block of code:
Change the 1 in this line:
ThisWorkbook.Sheets(1).Range("A1") = ReturnWhenModifiedDate
to the name of the worksheet you want to update in quotes.
If your worksheet was named Second Trial then the first block of code would be:
Rich (BB code):Option Explicit Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) ThisWorkbook.Sheets("Second Trial").Range("A1") = ReturnWhenModifiedDate End Sub
All four lines of the edited first block of code goes in the "ThisWorkbook" code page.
Insert a new module, in the VBE menu select Insert | Module and place the second block of code in it.
Nothing needs to go in the code page of the worksheet that you want to have updated.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'If any cell on a worksheet is changed manually or progrmatically, update date/time in cell A1 of tha worksheet
Application.EnableEvents = False
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
Application.EnableEvents = True
End Sub
Add this code to the code page for each worksheet that you want to track:
Code:Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'If any cell on a worksheet is changed manually or progrmatically, update date/time in cell A1 of tha worksheet Application.EnableEvents = False With Range("A1") .Value = Now() .NumberFormat = "mm/dd/yyyy hh:mm:ss" End With Application.EnableEvents = True End Sub
This will trigger each time the sheet is changed, not only when the workbook is saved.
With Range("B1")
.Value = Environ("Username")
End With