Insert a Timestamp When a Macro Is Run in Excel VBA – 3 Steps

Overview:

Sub Timestamp_When_a_Macro_is_Run()

Timestamp_Sheet = “Sheet1”
Timestamp_Column = “B”

Set Timestamp_Range = Worksheets(Timestamp_Sheet).Range(Timestamp_Column + Right(Str(1), 1))
i = 1
While Timestamp_Range.Cells(i, 1) <> “”
i = i + 1
Wend

Timestamp_Range(i, 1) = Now
Timestamp_Range(i, 1).NumberFormat = “m/d/yyyy h:mm:ss AM/PM”

End Sub

VBA Code to Timestamp When a Macro is Run in Excel VBA


Step 1 – Code Input

  • Enter the name of the worksheet to record the timestamps.
  • Enter the column.
Timestamp_Sheet = "Sheet1"
Timestamp_Column = "B"

Inserting Inputs to TimeStamp When a Macro is Run in Excel


Step 2 – Iterating Through a While-Loop to Find the Position of a New Timestamp

Set Timestamp_Range = Worksheets(Timestamp_Sheet).Range(Timestamp_Column + Right(Str(1), 1))
i = 1
While Timestamp_Range.Cells(i, 1) <> ""
    i = i + 1
Wend

Iterating to TimeStamp When a Macro is Run in Excel

Step 3 – Timestamps

Use the Now function and number formatting.

Timestamp_Range(i, 1) = Now
Timestamp_Range(i, 1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"

The complete VBA code is:

 VBA Code:

Sub Timestamp_When_a_Macro_is_Run()

Timestamp_Sheet = "Sheet1"
Timestamp_Column = "B"

Set Timestamp_Range = Worksheets(Timestamp_Sheet).Range(Timestamp_Column + Right(Str(1), 1))
i = 1
While Timestamp_Range.Cells(i, 1) <> ""
    i = i + 1
Wend

Timestamp_Range(i, 1) = Now
Timestamp_Range(i, 1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"

End Sub

VBA Code to Timestamp When a Macro is Run in Excel VBA


Developing the Macro to Insert Timestamps When It Is Run

Step 1 – Opening the VBA Window

  • Press ALT + F11 to open the Visual Basic window.

Opening the VBA Window to TimeStamp When a Macro is Run in Excel

Step 2 – Inserting a New Module

  • Go to Insert > Module.

Step 3 – Enter the VBA Code

Putting the VBA Code to TimeStamp When a Macro is Run in Excel

Step 4 – Run the Code

  • Click Run Sub / UserForm.

The code will record the timestamps in the selected column (Column B, here).

Read More: Now and Format Functions in Excel VBA


Things to Remember

“m/d/yyyy h:mm:ss AM/PM” was used in the code. You can change the date format.


Download Practice Workbook

Download the practice workbook.

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo