WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Blog
  • Free Video Tutorials
  • Courses
  • All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Now

VBA Now

By Jeevan A YJeevan A Y | Reviewed By Dheeraj VaidyaDheeraj Vaidya, CFA, FRM

Excel VBA Now Function

NOW is a date and time function in both VBA which is used to get the current system date and time, just like the worksheet function which does not take any arguments in it, in VBA now function also does not takes any arguments, the return output for this function is date.

VBA NOW function is similar to the one in the excel worksheet function. Like the DATE function in VBA, “NOW” too doesn’t have parameters to pass in. We just need to pass the function with closed parenthesis or no need for parenthesis as well. By using the DATE function in VBA, we can generate the current date as showing the system we are working on. However, I have seen situations where we need current time along with the date as well. In excel, we can do many varieties of things; similarly, we can generate current date and time with a simple function called NOW in excel.

VBA Now

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. Follow the below steps to write the VBA code and have a fair bit of knowledge on NOW as well as writing the code.

Step 1: Start the subprocedure by giving a macro name.

Code:

Sub Now_Example1()

End Sub

VBA Now Example 1

Step 2: Declare the variable as “Date.” The reason why we need to declare the variable as “Date” because our end result is in Date and Time format.

Code:

Sub Now_Example1()

Dim k As Date

End Sub

Example 1-1

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

Example 1-2

Step 4: Now show the value of the function NOW, which we have assigned to the variable “k” in the message box in VBA.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

Code:

Sub Now_Example1()

Dim k As Date

k = Now

MsgBox k

End Sub

VBA Now Example 1-3

Ok, we are done.

Run the code using the F5 key or manually and see what the result is.

VBA Now Example 1-4

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

Example 1-5

Run the code and see the difference.

VBA Now Example 1-6

Now we have a proper 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, and in the second example, we go 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

Example 2

In the green-colored area, copy and paste your code.

Execute the code by pressing the F5 key or press 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.

VBA Now Example 2-1

Like this, we can use NOW function in many ways in VBA.

You can download this Excel VBA Now Function template here – VBA Now Function Template

Recommended Articles

This has been a guide to VBA Now. Here we learn how to use Now function in Excel VBA and its alternative to timer Function along with simple to advanced examples. Below are some useful excel articles related to VBA –

  • VBA Change Font Color
  • Sleep in VBA
  • For Each Loop in VBA
  • Data Type in VBA
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

Special Offer - VBA Training Course (6 courses, 35+ hours video) View More