Objective of the VBA Now Function:
The VBA Now Function is a built-in function in MS Excel. It does not take any input arguments or parameters. It returns the current system date and time. It is a ‘Data and Time’ type function. Default format of the Now Function is ‘mm/dd/yyyy HH:MM:SS AM/PM’. The Now function we can use in worksheet as well. The VBA Now Function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA Now Function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Now Function, where we can use this VBA Now Function, real-time examples and Instructions.
Table of Contents
- Objective of Now Function
- Syntax of VBA Now Function
- Parameters or Arguments
- Where we can apply or use the VBA Now Function?
- Example 1: Display current system date and time on the screen
- Example 2: Display current system date and Time on the Worksheet
- Example 3: Change Date and Time Format
- Instructions
- Download File
Syntax of VBA Now Function
The syntax of the VBA Now Function is
NOW()
In the above syntax parentheses is optional. If there is no argument, then no need to specify parentheses.
Parameters or Arguments
There are no parameters or arguments for the Now Function.
Where we can apply or use the VBA Now Function?
We can use this VBA date function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Display current system date and time on the screen
Here is a simple example of the VBA Now Function. This below example macro uses the Now Function and displays the current system date and time.
'Display Current System Date and Time on the screen Sub VBA_Now_Function_Ex1() 'Variable declaration Dim sCurrentDateTime As Date 'Assign current system date to variable sCurrentDateTime = Now() MsgBox sCurrentDateTime, vbInformation, "Current System Date & Time" End Sub
In the above example ‘sCurrentDate’ declared as a Date data type. This variable ‘sCurrentDate’ now contains the current system date and time.
Output:
Here is the screen shot of first example output.
Example 2: Display current system date and Time on the Worksheet
Here is another example of the VBA Now Function. This below example macro uses the Now Function and displays the current system date and time on the Worksheet named ‘VBAf1.com’ in Range B18.
'Display Current System Date and Time on the Worksheet Sub VBA_Now_Function_Ex2() 'Variable declaration Dim sCurrentDateTime As Date 'Assign current system date to variable sCurrentDateTime = Now Sheets("VBAF1.com").Range("B18") = sCurrentDateTime 'or 'Sheets("Sheet1").Cells(18, 2) = sCurrentDateTime End Sub
Output:
Here is the screen shot of second example output.
Example 3: Change Date and Time Format
Here is one more example with VBA Now Function. This below example macro uses the Now Function and changes the format of the date and time. Finally, it displays the current system date and time on the screen.
'Format and Display Current System Date and Time on the screen Sub VBA_Now_Function_Ex3() 'Variable declaration Dim sCurrentDateTime As Date 'Assign current system date to a sCurrentDateTime = Now MsgBox Format(sCurrentDateTime, "DD/MMM/YYYY HH:MM:SS"), vbInformation, "Current System Date & Time" End Sub
Output:
Here is the screen shot of third example output.
Instructions
Here are the instructions to use the VBA Now function in Excel
- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Insert above specified macro example.
- Select code and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
- Ex1-Output: It displays current system date and time on the screen.
- Ex2-Output: It displays current system date and time on the Worksheet in Rage B19.
- Ex3-Output: It displays current system date and time on the screen.
- Note: Difference between first output and second output is format of the date and time.
Download File
Click on following link to download free example excel workbook to learn more about VBA Now function.
List of VBA Functions:
Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions