Excel VBA Now Function

The VBA NOW function is similar to the one in the Excel worksheet function. Like the DATE function in VBADATE Function In VBAVBA Date is a date and time function. It returns only the current date as per the system date you are using and has no arguments whatsoever. This function returns the current system date.read more, 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 excelFunction Called NOW In ExcelIn an excel worksheet, the NOW function is used to display the current system date and time. The syntax for using this function is quite simple =NOW ().read more.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Now (wallstreetmojo.com)

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 codeWrite The VBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more 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.

Sub Now_Example1()

Dim k As Date

End Sub

Step 3: Assign the value to the variable “k” by applying VBA NOW function.

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 VBAMessage Box In VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided.read more.

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.

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 TIMERVBA TIMERVBA timer is an inbuilt function to give us the fractional value of seconds. Sometimes, it is used to pause any set of codes running or resume them based on the time provided. A timer is used as a statement in VBA with the input of time.read more, 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

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: –

  • VBA Change Font ColorSleep in VBAFor Each Loop in VBAData Type in VBA