The VBA Date function in Excel is a built-in function in MS Excel. It does not take any input arguments or parameters. It returns the current system date. It is a ‘Data and Time’ type function. Default format of the Date function is ‘mm/dd/yyyy’.
The VBA Date function use in either procedure or function in a VBA editor window in Excel. We can use the VBA date 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 date function, where we can use this VBA date function and real-time examples.
- Objective
- Syntax of VBA Date Function:
- Parameters or Arguments:
- Where we can apply or use the Date Function?
- Example 1: Display current system date on the screen
- Example 2: Display current system date on the Worksheet
- Example 3: Change Date Format
- Download File
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA Date Function:
The syntax of the VBA Date function is
Date()
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 Date function.
Where we can apply or use the Date 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 on the screen
Here is a simple example of the VBA Date function. This below example macro uses the Date function and displays the current system date.
Sub VBA_Date_Function_Ex1() 'Display Current System Date on the screen 'Variable declaration Dim sCurrentDate As Date 'Assign current system date to variable sCurrentDate = Date MsgBox sCurrentDate, vbInformation, "Current System Date" End Sub
In the above example ‘sCurrentDate’ declared as a Date data type. This variable ‘sCurrentDate’ now contains the current system date.
Output: Here is the screen shot of first example output.
Example 2: Display current system date on the Worksheet
Here is another example of the VBA Date function. This below example macro uses the Date function and displays the current system date on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.
Sub VBA_Date_Function_Ex2() 'Display Current System Date on the Worksheet 'Variable declaration Dim sCurrentDate As Date 'Assign current system date to variable sCurrentDate = Date Sheets("Sheet1").Range("B18") = sCurrentDate 'Or 'Sheets("Sheet1").Cells(18, 2) = sCurrentDate End Sub
Output:
Here is the screen shot of second example output.
Example 3: Change Date Format
Here is one more example with VBA Date function. This below example macro uses the Date function and changes the format of the date. Finally, it displays the current system date on the screen.
Sub VBA_Date_Function_Ex3() 'Format and Display Current System Date on the screen 'Variable declaration Dim sCurrentDate As Date 'Assign current system date to a variable sCurrentDate = Date MsgBox Format(sCurrentDate, "DD/MMMM/YYYY"), vbInformation, "Current System Date" End Sub
Output: Here is the screen shot of third example output.
Download File
Click on following link to download free example excel workbook to learn more about VBA Now function.
Watch at YouTube:
You can also watch in youtube.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers