You can use the IsDate function in VBA to check if a given cell is a date.
This function will return True if the value in a given cell is recognized as a date.
Otherwise, the function will return False.
Here is one common way to use this function in practice:
Sub CheckDate()
Dim i As Integer
For i = 1 To 9
If IsDate(Range("A" & i)) = True Then
Range("B" & i) = "Is a Date"
Else
Range("B" & i) = "Is Not a Date"
End If
Next i
End Sub
This particular macro will check if each cell in the range A1:A9 is a date.
If a cell is a date, then “Is a Date” will be returned in the corresponding cell in the range B1:B9.
If a cell is not a date, then “Is Not a Date” will be returned instead.
The following example shows how to use this syntax in practice.
Example: How to Use IsDate in VBA
Suppose we have the following column of values in Excel:
Suppose we would like to check if each cell in column A is a date.
We can create the following macro to do so:
Sub CheckDate()
Dim i As Integer
For i = 1 To 9
If IsDate(Range("A" & i)) = True Then
Range("B" & i) = "Is a Date"
Else
Range("B" & i) = "Is Not a Date"
End If
Next i
End Sub
When we run this macro, we receive the following output:
Column B tells us whether or not each corresponding cell in column A is recognized as a date or not.
Note that we chose to return either “Is a Date” or “Is Not a Date” but you can return whatever you would like using the If Else statement.
Note: You can find the complete documentation for the VBA IsDate function here.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Get Month Name from Date
VBA: How to Use DateValue Function
VBA: Countif Greater than Date