In this article, I’ll show you how you can keep a timestamp in Excel VBA every time a Macro is run.
Excel VBA: Insert Timestamp When a Macro Is Run (Quick View)
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
How to Insert Timestamp When a Macro Is Run with Excel VBA: Step-by-Step
So, without further delay, let’s go to our main discussion today. We’ll break down the VBA code step-by-step to learn how to timestamp when a Macro runs using VBA.
Actually in VBA, timestamping is easy. VBA provides a function called Now that returns the current time each time a Macro is run. The function along with some number formatting can provide you an excellent timestamping every time you run your code.
But there’s an additional task here. We want to keep a record of all the instants when the Macro is run. Therefore, we’ve to arrange the code in a way such that it can keep a record of all the instants. We’ll use a while-loop for this purpose.
In a nutshell, there are a total of 3 steps to developing the code.
⧪ Step 1: Inserting Necessary Inputs
At the very outset of the code, we have to insert the necessary inputs into the code. The inputs include:
- The Name of the Worksheet Where You Want to Record the TimeStamps.
- The Column Inside the Worksheet
Timestamp_Sheet = "Sheet1"
Timestamp_Column = "B"
⧪ Step 2: Iterating Through a While-Loop to Find Out the Position of New Timestamp
Next, we’ll iterate through a while-loop to find the position of the 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
⧪ Step 3: Timestamping
This is the most important step. We’ll use the Now function of VBA along with some number formatting to timestamp, that is, to record the instant when the Macro is run.
Timestamp_Range(i, 1) = Now
Timestamp_Range(i, 1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"
So the complete VBA code will be:
⧭ 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
Read More: How to Insert Excel Timestamp When Cell Changes Without VBA
Developing the Macro to Insert Timestamp When It Is Run
We’ve seen the step-by-step analysis of the code to timestamp when a Macro runs.
Now let’s see how to develop the Macro with the code.
⧪ Step 1: Opening VBA Window
Press ALT + F11 on your keyboard to open the Visual Basic window.
⧪ Step 2: Inserting a New Module
Go to Insert > Module in the toolbar. Click on Module. A new module called Module1 (or anything else depending on your past history) will open.
⧪ Step 3: Putting VBA Code
This is the most important step. Insert the given VBA code in the module.
⧪ Step 4: Running the Code
Click on the Run Sub / UserForm tool from the toolbar above.
The code will run recording the current instant, in a new cell of your desired column (Column B here).
Read More: How to Insert Timestamp in Excel When Cell Changes
Things to Remember
In this code, we’ve used the most common format to record a date and time together (“m/d/yyyy h:mm:ss AM/PM”). If you wish, you can change the format. Here you can have a look at all the time formats available in VBA.
Download Practice Workbook
Download this practice workbook to exercise while you are reading this article.
Conclusion
Therefore, this is the process to timestamp each time a Macro runs using Excel VBA. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.
Related Articles
- How to Insert Current Date and Time in Excel
- How to Combine Date and Time in One Cell in Excel
- How to Auto Update Current Time in Excel
- Excel Current Time Formula
- How to Insert the Current Date and Time in Cell A1
- How to Add Date and Time in Excel When Printing
- How to Insert Last Modified Date and Time in Excel Cell
- How to Enter Date and Time in Excel