OK so you want to automatically date and time stamp your data entry in Microsoft Excel. In this video I will demonstrate two methods you can use to achieve this. The first method uses a formula and the second method uses a VBA macro.
Click here to download the featured file
Formula Method
Watch the video walk-though here: https://youtu.be/23-RoK1p7tg?t=52
This method ue an IF formula to return a date and time stamp.
The first step is to format the cells that will eventually contain your date and time stamps with the correct format.
- Select the cells
- Use the shortcut CTRL 1 or right-click Format Cells to open the Format Cells dialog box
- On the Number tab, select Custom at the bottom of the Category list
- In the Type box, type the following: dd/mm/yyyy hh:mm
- Click on OK to confirm
We can use the NOW() function to return the current date and time in Excel, but how do we stop the NOW() function changing the date and time whenever the worksheet is edited or refreshed?
The formula I used in this video to create the date and time stamp is =IF(A2<>””,IF(B2=””,NOW(),B2),””) This formula only records the date and time of the original entry and will not update if changes are subsequently made.
A2 is the cell that contains the data and B2 is the cell that will contain the date and time stamp. This formula creates a circular reference as it refers to cell B2, which is the cell the formula is in. Because of this you need to change a setting in Excel to get the formula to work.
To change the setting in Excel:
- Click on the ribbon’s File tab
- Select Options (bottom left of screen)
- Select Formulas (on the left)
- In the Calculation options section (first section) tick Enable iterative calculations
- Change the Maximum Iterations setting to 1
- Click OK to confirm
VBA Macro Method
Watch the video walk-through here: https://youtu.be/23-RoK1p7tg?t=244
The second method uses a VBA macro to return a date and time stamp. This method will record the date and time of the original entry but will also record the date and time of the most recent update in a separate column.
As we are using a VBA macro for this method, your Excel workbook will need to be saved as a macro-enabled workbook.
You will also want to show the Developer tab on the ribbon. To do that:
- Right-click on any visible ribbon tab and select Customize the Ribbon
- Tick Developer in the list of Main Tabs
- Click OK to confirm
The next step is open the Visual Basic Editor (VBE). You can open the VBE by either clicking the Visual Basic button on the Developer tab or by using the shortcut ALT F11.
The macro we are running needs to be triggered by the worksheet ‘change’ event.
- In the Project Explorer (on the left), double-click the worksheet you are working in and want the macro to apply to
- Above the Code window, change the first drop-down from (General) to Worksheet
- In the second drop-down select Change
- You can then delete the last two lines of code that appear in the Code window
Between the two lines of remaining code, paste the following:
Dim MyData As Range Dim MyDataRng As Range Set MyDataRng = Range("A2:A10") If Intersect(Target, MyDataRng) Is Nothing Then Exit Sub On Error Resume Next If Target.Offset(0, 1) = "" Then Target.Offset(0, 1) = Now End If Target.Offset(0, 2) = Now For Each MyData In MyDataRng If MyData = "" Then MyData.Offset(0, 1).ClearContents MyData.Offset(0, 2).ClearContents End If Next MyData
For a detailed walk-through of the code, please see the video above.