Excel VBA Time Function
VBA Time Function returns the current time, also the important thing to note is that this function has no arguments in it whatsoever, another important factor to remember is that this function returns the current system time. Using this function we can actually find the actual time taken by the line of codes to complete the process.
TIME is a kind of volatile function. It does not have any syntax to it.
We also have a similar function in excel – NOW () function, which inserts both current times as well as the current date in the spreadsheet.
We just need to enter the function. In fact, no need for parenthesis to enclose just TIME is enough to insert the current time. The result given by the TIME function is in the string.
How to Use TIME Function in VBA?
Let me show you the example of a simple TIME in excel function. Follow the below steps to create code to use the TIME function.
Step 1: Create a macro.
Sub Time_Example1() End Sub
Step 2: Declare a variable as String.
Sub Time_Example1() Dim CurrentTime As String End Sub
Step 3: Assign a value to this variable through the TIME function.
Sub Time_Example1() Dim CurrentTime As String CurrentTime = Time End Sub
Step 4: Now show the result in the message box.
Sub Time_Example1() Dim CurrentTime As String CurrentTime = Time MsgBox CurrentTime End Sub
Run this code using the F5 key or manually. We will get the current time.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
So, when I run this code, the time was 11.51.54 AM.
Alternative of Now() Function
Combination of Date & Time as an Alternative to NOW Function
As I told at the beginning of the article, NOW can insert the current date & time. However, we can use two other functions as an alternative function to the NOW function; those two functions are VBA DATE & VBA TIME functions.
VBA Date will return the current date & Time will return the current time, so this makes the NOW function. Below is a set of code which will insert the current date & time in cell A1.
Sub Time_Example2() Range("A1").Value = Date & " " & Time End Sub
This code will insert the current date & time in cell A1.
We can also apply a format to these values using the FORMAT function. The below code will format the date & time.
Sub Time_Example2() Range("A1").Value = Date & " " & Time Range("A1").NumberFormat = "dd-mmm-yyyy hh:mm:ss AM/PM" End Sub
Now the result of this code is as follows.
Track Your Workbook Open Records using Time Function in VBA
Often times, we need to know our workbook opening time-frequency. There is a situation where we open the workbook quite often, and we make some changes. By tracking the workbook opening time & date, we can track workbook opening time.
Create a new sheet and rename it as “Track Sheet.”
Step 1: Double click on This workbook from VBE Editor.
Step 2: Select the workbook from the object drop-down list.
Step 3: As soon as you select this option, you can see a new macro automatically created by itself in the name “Workbook_Open().”
Step 4: Inside this macro, we will need to write a code to track the workbook opening date & time.
I have already written code, and below is the code for you.
Private Sub Workbook_Open() Dim LR As Long LR = Sheets("Track Sheet").Cells(Rows.Count, 1).End(xlUp).Row + 1 Sheets("Track Sheet").Cells(LR, 1).Value = Date & " " & Time() Sheets("Track Sheet").Cells(LR, 1).NumberFormat = "dd-mmm-yyyy hh:mm:ss AM/PM" End Sub
This will record your workbook opening times like the below one.
This has been a guide to VBA Time Function. Here we learned how to use VBA Time Function, and also we learned the Combination of date and time as an alternative to Now() Function and tracks the workbook open records along with examples and downloadable excel sheet. Below are some useful excel articles related to VBA –