VBA DATEVALUE Function

VBA DATEVALUE Function

Objective of the VBA DateValue Function :

The VBA DateValue Function is a built-in function in MS Excel. It has one input argument or parameter. It returns the date value representing date for a string. It is a ‘Data and Time’ type function. It can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA DateValue 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 DateValue function.We will also see where we can use this VBA DateValue Function and real-time examples.

Table of Contents:

Syntax of VBA DateValue Function

The syntax of the VBA DateValue Function is
DATEVALUE(date_value)

Parameters or Arguments

There is one parameter or argument for the DateValue Function in VBA. That is nothing but ‘date_value’ parameter. It is a mandatory argument. It represents date in string format.

Where we can apply or use the VBA DateValue Function?

We can use this DateValue Function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007 and Excel 2003. We can also use in 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: Convert the given input string to a valid date and display on the screen

Here is a simple example of the VBA DateValue Function. This below example macro uses the DateValue Function and displays date for the specified string. In this case specified string represents date.

Sub VBA_DATEVALUE_Function_Example1()
'Convert the given input string to a valid date and display on the screen
    
    'Variable declaration
    Dim dDate As Date
    
    'Assign date to a variable
    dDate = DateValue("May 20, 2017")
    
    'Display output on the screen
    MsgBox "Specified Date is: " & dDate, vbInformation, "VBA DATEVALUE Function"
    
End Sub 

In the above example ‘dDate declared as ‘Date’ data type. And DATEVALUE is a vba function. This variable ‘dDate’ now contains the date.
Output:
Here is the screen shot of first example output.
VBA DATEVALUE Function Example 1

Example 2: Convert the given input string to a valid time and display on the Worksheet

Here is another example of the VBA DateValue Function. This below example macro uses the DateValue Function and displays date for the specified string(date). Now, it displays on the Worksheet named ‘VBAF1.Com’ at Range ‘B20’.

Sub VBA_DATEVALUE_Function_Example2()
'Convert the given input string to a valid date and display on the Worksheet
    
    'Variable declaration
    Dim dDate As Date
    
    'Assign date to a variable
    dDate = DateValue("May 20, 2017")
    
    'Display output on the screen
    Sheets("VBAF1.COM").Range("B20") = "Specified Date is: " & dDate
    
End Sub 

Output:
Here is the screen shot of second example output.
VBA DATEVALUE Function Example 2

Instructions:

Here are the instructions to use the DateValue Function in Excel

  1. Open an Excel workbook.
  2. Press Alt+F11 to open VBA Editor window.
  3. Go to Insert menu and click on module from the available options.
  4. Insert above specified macro example.
  5. Select code and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
  6. Ex1-Output: It displays date on the screen.
  7. Ex2-Output: It displays date on the Worksheet at Rage B20.

Download File:

Click on following link to download free example excel workbook to learn more about the DateValue Function.
VBA DateValue Function Examples

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *