Excel VBA Time
VBA TIME function returns the current time as output and can also be used as an input to different macro lines of code. This function does not have any parameter set for it and thus, by far has the simplest syntax as TIME word with empty parentheses which can be seen as below:
The syntax for VBA TIME function:
In fact, there is no need for parentheses as well, you can simply use the word TIME itself in your block of code which allows your system to return the current time. We also have a similar function called NOW() under Excel VBA which returns the current time along with the date. However, in this article, our entire focus would be on using TIME function and looking at different working examples of the same.
How to Use Excel VBA Time Function?
We will learn how to use a VBA Time function with few examples in excel.
VBA Time Function – Example #1
Current System Time Using TIME function:
Follow the below steps to display the current time of your system.
Step 1: Define a sub-procedure.
Sub TimeExample1() End Sub
Step 2: Define a variable which will be useful to hold the value of time for you. Define it as String.
Sub TimeExample1() Dim CurrentTime As String End Sub
Step 3: Use VBA TIME function to assign current time to the variable defined in the step above using an assignment operator.
Sub TimeExample1() Dim CurrentTime As String CurrentTime = Time End Sub
Step 4: As I mentioned earlier, there is no need to add parentheses after TIME function as this is a volatile function. Having said that, the following code will give the equivalent output in comparison with the above code.
Sub TimeExample1() Dim CurrentTime As String CurrentTime = Time() End Sub
Step 5: Now, use a MsgBox function to display the current time using the display message box.
Sub TimeExample1() Dim CurrentTime As String CurrentTime = Time() MsgBox CurrentTime End Sub
Step 6: Run this code by hitting F5 or Run button and see the output. You should get the output as shown in the image below.
Please note that the time reflecting in the image above is the time by which this code is run on the system. While trying it by yourself, your output may vary depending on the time you are working on this code.
VBA Time Function – Example #2
VBA TIME in combination with a Date function to return the current time with Today’s Date:
As it has been informed earlier in the introduction of this article, there is a function called NOW() which gives you current date as well as time in Microsoft Excel. See the screenshot given below.
However, in VBA we don’t have this function working. Therefore, in VBA we have to use a combination of VBA Date and VBA TIME functions to get the current date and current time. VBA Date will give a current date and VBA Time will give current time (which we already have seen in the 1st example).
Let’s see step by step how we can achieve this.
Step 1: Insert a new module in your Excel VBA script.
Step 2: Define a new sub-procedure by giving a name to the macro.
Sub Example2() End Sub
Step 3: We wanted our output to be stored in cell A1 of our excel sheet. For that, start writing the code as below.
Sub Example2() Range("A1").Value = End Sub
Step 4: Now, use a combination of VBA DATE and VBA TIME functions as shown below and assign it’s value to cell A1.
Sub Example2() Range("A1").Value = Date & " " & Time End Sub
The DATE function will return the current date of system and TIME function will return the current time. The white space enclosed in double quotes (“ “) allows having a space between date and time in the final output. Two & (and) operators work as concatenating operators which concatenate DATE and TIME with space in it.
Step 5: Run the code by hitting F5 or Run button manually. You will see the output in cell A1 of your worksheet as blow:
Here, again please note that the time reflecting in cell A1 is date and time by which this code is being run by myself. Therefore, when you’ll run this code the outputs will be different.
We also can format the date and time values using NumberFormat function under VBA.
Step 6: Write the code mentioned below in your VBA script.
Sub Example2() Range("A1").Value = Date & " " & Time Range("A1").NumberFormat = "dd-mmm-yyyy hh:mm:ss AM/PM" End Sub
Step 7: Hit F5 or Run button manually and you’ll see the output as below in cell A1 of your worksheet.
VBA Time Function – Example #3
VBA TIME function to track the opening time and date of a workbook:
Sometimes, there is one spreadsheet which we open frequently (Like Employee list of an organization) and make changes into it. We might be interested in knowing the date and time on which the changes have been made in the worksheet. VBA Time function can be used to track the date and time every time a workbook is opened. See the steps below to work on the same.
Step 1: Create a new worksheet in your Excel workbook and rename it as per your convenience. I will rename it as (Time_Track).
Step 2: Double click on ThisWorkbook under VBE (Visual Basics Editor).
Step 3: From object drop-down list select Workbook instead of General.
Step 4: You’ll see a new private sub-procedure will get defined under the name Workbook_Open() in VBE.
Private Sub Workbook_Open() End Sub
Step 5: Write down the below-mentioned code in this macro sub-procedure as shown in the image below:
Private Sub Workbook_Open() Dim LB As Long LB = Sheets("Time_Track").Cells(Rows.Count, 1).End(xlUp).Row + 1 Sheets("Time_Track").Cells(LB, 1).Value = Date & " " & Time() End Sub
Here a variable LB of type Long is defined to find out the cell next to last used cell where the output can be stored. Then, using a combination of VBA DATE and TIME functions, we have assigned a value under that cell.
Step 6: Run the code by hitting F5 or Run button manually and see the output under column A of a worksheet.
This code will store the value of Date and Time every time you open the worksheet Time_Track.
This is it from this article. Let’s wrap the things up by writing some things to remember.
Things to Remember
- VBA TIME function stores output as a string in excel. However, we also can store it as Date format while defining a variable as Date instead of a string.
- TIME function does not require any argument in it. It’s a volatile function.
- Even it does not need parentheses to call the function. Only TIME is enough to get the current time.
- TIME function by default stores time value in hh:mm:ss format (24 hours clock format). However, sometimes the output format depends on your system time format as well.
This has been a guide to VBA Time Function. Here we have discussed how to use Excel VBA Time Functions and also we learned the Combination of date and time as an alternative to Now() Function and tracks the workbook open records along with some practical examples and downloadable excel template. You can also go through our other suggested articles –