What is VBA DateValue Function?

A DateValue function is an in-built function in Excel VBA under the category of Date/time function. It works as both VBA functionVBA FunctionVBA functions serve the primary purpose to carry out specific calculations and to return a value. Therefore, in VBA, we use syntax to specify the parameters and data type while defining the function. Such functions are called user-defined functions.read more and worksheet function in VBAWorksheet Function In VBAThe worksheet function in VBA is used when we need to refer to a specific worksheet. When we create a module, the code runs in the currently active sheet of the workbook, but we can use the worksheet function to run the code in a particular worksheet.read more. This function returns the serial number or value of Date provided in the string representation format ignoring the information of time supplied by the Date string. It is used in two different ways in Excel. This function is used as a worksheet formula entering in a worksheet cell. It is used as the macro code in VBA application, entering it through Visual Basic Editor associated with Microsoft Excel.

In this article, we are going to learn examples of VBA DATEVALUE and how to use it with a clear explanation.


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

Explanation of VBA Datevalue Function

In VBA, DATEVALUE uses the following syntax.

VBA DateValue Formula

This function uses only the single Argument or Parameter

  • Date: It is the date represented in the string format
  • Returns: This function returns the value of the date when it is used as a VBA function. It returns the date value when it is used as a worksheet function

The VBA DateValue function is able to the interpretation of data represented in text format that is mentioned in a valid Excel format. It is not able to return the date value if the string included the text representation of weekday.

The benefits of the VBA DateValue function are extracting date value from string and converting date with time to the only date. We can simply that when a date is given with time, this function only date value avoiding the time value.

How to Use Excel VBA DATEVALUE?

To use the DateValue function in Excel, first needs to open the VBA editorOpen The VBA EditorThe Visual Basic for Applications Editor is a scripting interface. These scripts are primarily responsible for the creation and execution of macros in Microsoft software.read more.

The command button needs to be placed in the Excel worksheet to add the lines of the VBA program to it. To implement the lines of the program, the user requires to click on the command button in the excel sheet. To have a valid output from the program, valid input is given through the argument. For example, the following code helps the creation of a macro to run the DateValue function to extract the date value from the text in VBAText In VBAText is a worksheet function in excel but it can also be used in VBA while using the range property. It is similar to the worksheet function and it takes the same number of arguments. These arguments are the values which needs to be converted.read more.

VBA program: 

Sub Datebutton ()

Dim myDate As Date

myDate = DateValue (“ August 15, 1991”)

MsgBox Date (myDate)

End Sub

This code results in the date as 15 from the given input.

Examples of Excel VBA DATEVALUE

Below are the examples of DateValue in Excel VBA.

You can download this VBA DateValue Excel Template here – VBA DateValue Excel Template

Example #1 – Obtain Day, Month, and Year from a Date

Few steps are followed to create and execute the program in VBA. Those include

Step 1: Go to Developer Tab, place the cursor on a cell in the Excel sheet and click on the ‘Insert’ option and choose ‘Command Button’ under ActiveX Control, as shown in the figure.

VBA Datevalue Example 1

Drag the Button where you want and give the caption as Datebutton from the properties window.

VBA Datevalue Example 1-1

Step 2: Double Click on the button; it will redirect to the VBA project and write the code between the Private Sub-command button and the end sub.

The code should be developed as below to get a date, month, and year.


Private Sub Datebutton1_Click()

  Dim Exampledate As Date

  Exampledate = DateValue("April 19,2019")
  MsgBox Date
  MsgBox Year(Exampledate)
  MsgBox Month(Exampledate)

End Sub
VBA Datevalue Example 1-2

In this code, Datebutton1_Click() is the name and example date are variable with Date data type and Msgbox to display the output.

Step 3: While developing code, VBA type mismatch errors Type Mismatch ErrorsWhen we assign a value to a variable that is not of its data type, we get Type mismatch Error or Error code 13. For example, if we assign a decimal or long value to an integer data type variable, we will get this error (Error Code 13) when we run the code.read more will be occurred and needs to take care of them.

Step 4: In this step, run the program clicking on the run option.

VBA Datevalue Example 1-3.png

Or we can check or debug the program step by step, selecting the Step Into option under the Debug menu. If our code is without any errors, it displays the output.

VBA Datevalue Example 1-4

Step 5: When the program is executed, it first displays the message box with the date given in the text input. Next, click on OK to see the year value again, click OK on the message box to see the month value.

VBA Datevalue Example 1-5
Note: These steps are to be followed clearly to have accurate results.

Example #2 – Using the DatePart to Get the Different Parts of Date

Step 1: Go to Excel Developer TabExcel Developer TabEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more, place the cursor on a cell in the Excel sheet and click on the ‘Insert’ option and choose ‘Command Button’ under ActiveX Control, as shown in the figure.

Datepart Example 2

Step 2: Drag the button and give the caption as DatePart under properties.

datepart Example 2-1

Double click on this button. It directs to the Visual Basic Editor sheet and displays as follows.

Step 3: Develop the code using the DatePart with DateValue, as shown in the figure.


Private Sub Datepart1_Click()

 Dim partdate As Variant
 partdate = DateValue("8/15/1991")
 MsgBox partdate
 MsgBox Datepart("yyyy", partdate)
 MsgBox Datepart("dd", partdate)
 MsgBox Datepart("mm", partdate)
 MsgBox Datepart("q", partdate)

End Sub
Example 2-2

In this program, DatePart1 is the macro name, and partDate is the argument name with data type ‘variant.’ For displaying year, date, month, and a quarter, the format is applied as “yyyy,” “d,” “m,” and “q.” If we make any mistake in the format, it displays the following error.

Example 2-3

Step 4: After successful debugging of the program, run the program by clicking on the run button to use excel shortcut keyUse Excel Shortcut KeyAn Excel shortcut is a technique of performing a manual task in a quicker way.read more F5.

The code first displays the full date, and then after clicking every OK from the msgbox, it shows the year value after that Date value, Month Value, Quater Value, respectively.

VBA Datevalue Example 2-4

Things to Remember About the VBA DATEVALUE

The following things must be remembered while using the DateValue function in Excel VBA

  • Run time error 13 with message Type Mismatch is displayed when the date provided to the DateValue function is not able to convert into a valid date. We need date is a proper text format
  • When we try to get the only date from the string argument with code ‘msgbox date (argument name),’ it displays the type mismatch error.
  • We can see the output of the DateValue function without opening the VBA editor. It is done by clicking the command button and select the macro created for the respective program.
  • When DatePart is used to get the values, the appropriate format should be followed. Otherwise, it leads ‘run time error 5’ with message invalid procedure call or argument.

Recommended Articles

This has been a guide to VBA DateValue. Here we will learn how to use VBA DateValue function to returns the serial number or value of Date provided in the string along with practical examples and a downloadable template. Below you can find some useful excel VBA articles –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion