Table Of Contents
Excel VBA IsDate Function
IsDate is the VBA function that tests whether the given value is the date or not. If the supplied value or range reference value is the date value, we will get the result as “TRUE.” On the other hand, if the value is not date, we will get the result as “FALSE.” So, the result is a BOOLEAN value, i.e., TRUE or FALSE.
Table of contents
Below is the syntax of the IsDate function.
The expression is nothing but the value we are trying to test, whether it is the date or not.
How to use the VBA IsDate Function?
We will test whether the value “5.01.19” is a date value or not.
For this first start, the excel macro procedure.
Code:
Sub IsDate_Example1() End Sub
Define the variable to store the date value, and since the value will be the date value, assign the data type as “Date” only.
Code:
Sub IsDate_Example1() Dim MyDate As Date End Sub
Now assign the value of “5.1.19” to the variable “MyDate.”
Code:
Sub IsDate_Example1() Dim MyDate As Date MyDate = "5.1.19" End Sub
Open the message box in VBA now.
Code:
Sub IsDate_Example1() Dim MyDate As Date MyDate = "5.1.19" MsgBox( End Sub
In this message box, we will test whether the supplied date value to the variable “MyDate” is the date or not by using the “IsDate” function. First, open the “IsDate” function.
Code:
Sub IsDate_Example1() Dim MyDate As Date MyDate = "5.1.19" MsgBox IsDate( End Sub
The expression is the value we are testing to find whether it is Date or not. Since we have already stored the value to the variable “MyDate,” supply the variable name only.
Code:
Sub IsDate_Example1() Dim MyDate As Date MyDate = "5.1.19" MsgBox IsDate(MyDate) End Sub
Now run the code and see what we get in the message box.
The result is TRUE.
You must wonder how it recognized the value “5.1.19” as the date.
It has returned the result as TRUE because when you look at the given value “5.1.19,” it is the short form of the date “05.01.2019,” so Excel is brilliant enough to recognize it as a date, so the result is TRUE.
Now here comes the tricky thing, for the same value, what we will do is we will change the short form of the year from 19 to 2019.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "5.1.2019" MsgBox IsDate(MyDate) End Sub
Now, run the code and see the result.
This time it has returned the result as FALSE because the “day and month” portion of the date is in short form, but the year part is in full form of “YYYY,” so ISDATE cannot recognize it has a date, so the result is FALSE.
Now, look at the code below.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "05.01.2019" MsgBox IsDate(MyDate) End Sub
We have mentioned a full day and full month format by using 0. Let us run the code and see the result of the IsDate function.
This time also we go got the result as FALSE.
Now, change the code as follows.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "05/01/2019" MsgBox IsDate(MyDate) End Sub
Instead of the dot (.) as the separator, we have entered forward-slash (/) as the separator. Now run the code and see the result.
This time we got the result as TRUE.
We have told you at the beginning of the article that “Date” is a sensitive thing.
Now what we will do is we will merge the date and time.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "05/01/2019 15:26:24" MsgBox IsDate(MyDate) End Sub
We have added above the time portion of “15:26:24” in front of the date. Now, run the code and see the result.
This time, we also got the result TRUE because DATE and TIME in Excel are the same things and stored as serial numbers. The whole number represents the date portion, and decimal places represent the time portion.
Things to Remember here
- The IsDate function returns the Boolean type result, i.e., TRUE or FALSE.
- The IsDate function is available only as a VBA function.
- Only valid formatted dates one can treat as the date. Else, it will treat as text values and return the result as FALSE.
Recommended Articles
This article has been a guide to VBA IsDate. Here, we discuss how to use the VBA IsDate function to check whether a given value is a date or not, along with the example and downloadable Excel sheet. Below are some useful Excel articles related to VBA: -