VBA Now
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
Excel VBA Now Function
NOW is a DATE and TIME function in both VBA used to get the current system date and time. Like the worksheet function, which does not take any arguments, in VBA, the NOW function does not take any arguments. The return output for this function is the date.
The VBA NOW function is similar to the one in the Excel worksheet function. Like the DATE function in VBA, the NOW function does not have parameters to pass in. We need to pass the function with closed parenthesis or not need for parenthesis. Using the DATE function in VBA, we can generate the current date showing the system we are working on. However, we have seen situations where we need the current time and date. In Excel, we can do various things. Similarly, we can generate the current date and time with a simple function called NOW in excel.
Table of contents
The formula of the VBA NOW is simple.
NOW ()
Example of NOW Function in VBA Excel
Look at the simple example of the NOW function in VBA. Then, follow the below steps to write the VBA code Have a fair bit of knowledge on NOW and writing the code.
Step 1: Start the sub procedure by giving a macro name.
Code:
Sub Now_Example1()
End Sub
Step 2: Declare the variable as "Date." We need to declare the variable as "Date" because our result is in the date and time format.
Code:
Sub Now_Example1() Dim k As Date End Sub
Step 3: Assign the value to the variable “k” by applying VBA NOW function.
Code:
Sub Now_Example1() Dim k As Date k = Now End Sub
Step 4: Now, show the value of the NOW function assigned to the variable "k" in the message box in VBA.
Code:
Sub Now_Example1() Dim k As Date k = Now MsgBox k End Sub
We have completed it now.
Run the code using the F5 key or manually and see the result.
The result shows 4/15/2019 at 5:03:35.
My computer date format is “mm-dd-yyyy.”
We can also change the date format by using the FORMAT function. Below is the code to change the date format.
Code:
Sub Now_Example1() Dim k As Date k = Now MsgBox Format(k, "DD-MMM-YYYY HH:MM:SS") End Sub
Run the code and see the difference.
Now, we have an accurate date and time format. With this format, anybody can understand the date and time format.
Volatile in Nature:
As you can see in the first example, we got the time result as 5:03:35. In the second example, we got the result as 17:19:02. So this shows that the NOW function is a volatile function that changes every second.
Alternative to Timer Function in VBA
As an alternative to VBA TIMER, we can use the “VBA NOW” function to calculate the total time taken by the macro to complete the task.
Use the below code to calculate the time taken by your code.
C0de:
Sub TotalDuration() Dim k As Date k = Now ' ' 'Enter your code here ' ' ' MsgBox "Total Time Taken by the macro to complete the task is : " & _ Format((Now - k), "HH:MM:SS") End Sub
In the green-colored area, copy and paste your code.
Execute the code by pressing the F5 key or the run button. As soon as it completes the execution, we will get the time taken by the macro to complete the task message in the message box. Below is an example of the same.
Like this, we can use the NOW function in many ways in VBA.
You can download this Excel VBA Now Function template here – VBA Now Function Template
Recommended Articles
This article has been a guide to VBA Now. Here, we learn how to use the Now function in Excel VBA, its alternative to the time function, and simple to advanced examples. Below are some useful Excel articles related to VBA: -