Excel VBA IsDate Function
IsDate is the VBA function which tests whether the given value is the date or not. If the supplied value or range reference value is date value then we will get the result as “TRUE”, if the value is not date value then we will get the result as “FALSE”. So, the result is BOOLEAN value i.e. either TRUE or FALSE.
Below is the syntax of the IsDate function.
The expression is nothing but the value that we are trying to test, whether it is the date or not.
How to use 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 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.
4.9 (1,353 ratings) 35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
Code:
Sub IsDate_Example1() Dim MyDate As Date MyDate = "5.1.19" MsgBox IsDate(MyDate) End Sub
Ok, now run the code and see what we get in the message box.
Wow!!! The result is TRUE.
You must be wondering how it recognized the value “5.1.19” as the date.
The reason why 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 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 below code.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "05.01.2019" MsgBox IsDate(MyDate) End Sub
I have mentioned a full day and full month format by using 0. Let’s 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.
This is the reason I have told you at the beginning of the article that “Date” is a sensitive thing.
Now what I will do is I will merge the date and time together.
Code:
Sub IsDate_Example1() Dim MyDate As String MyDate = "05/01/2019 15:26:24" MsgBox IsDate(MyDate) End Sub
What I have added above is the time portion of “15:26:24” in front of the date. Now run the code and see the result.
This time too, we got the result as TRUE because DATE & 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
- IsDate returns the Boolean type result, i.e., either TRUE or FALSE.
- IsDate is available only as a VBA function.
- Only valid formatted dates are treated as the date, or else it will treat as text values and return the result as FALSE.
Recommended Articles
This 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 –
- 35+ Courses
- 120+ Hours
- Full Lifetime Access
- Certificate of Completion