[VBA] How to Check if a Cell Value is a Date | Using the IsDate Function

In Excel VBA, there are many situations where you need to determine if a value entered in a cell is a valid “date”.

This is useful for checking the validity of user input, processing calendars, or verifying data consistency during aggregation.

This article explains how to use the IsDate function in VBA to check if a cell value is a date.

目次

Basic Syntax of IsDate

IsDate(Value)
  • It returns True if the value is recognized as a valid date or time.
  • Otherwise, it returns False.

Code Example: Checking a Range of Cells

The following code checks the values entered in the range “B2:B6” and displays whether they are dates in the cell immediately to the right.

Sub CheckIfDate()
    Dim checkCell As Range
    
    ' Loop through each cell in the range B2:B6
    For Each checkCell In Range("B2:B6")
        ' Output the result (True/False) to the cell on the right
        checkCell.Offset(0, 1).Value = IsDate(checkCell.Value)
    Next
End Sub

Explanation

  • Range("B2:B6"): The range of cells you want to check as dates.
  • Offset(0, 1): This targets the cell one column to the right. The result is output here.
  • The result will be displayed as TRUE or FALSE in the Excel sheet.

Evaluation Examples (How IsDate Works)

Input ValueResult (IsDate)Note
2025/7/20TrueValid date format.
July 20TrueExcel automatically recognizes date formats (localized).
abcFalseA text string that cannot be recognized as a date.
44500TrueSerial numbers are also valid dates.
(Blank)FalseEmpty cells are not considered dates.

Common Use Cases

  • Input Validation: Checking if a user entered a valid date in a form.
  • Data Import: Verifying date formats when importing data from CSV or other sources.
  • Error Highlighting: Pre-processing macros to color cells that do not contain valid dates as a warning.

Summary

The IsDate function is extremely useful when you want to verify if a cell contains a date. However, please keep the following points in mind:

  • Serial Numbers: Excel treats dates as serial numbers (integers), so numbers like 44500 will return True. Be careful if you need to distinguish between dates and regular numbers.
  • Localized Formats: Text strings like “July 20” or other local date formats are True if Excel recognizes them.
  • Debugging: Outputting the result to a separate cell (as shown in the example) makes debugging and post-processing easier.

Using IsDate allows for concise judgment and is very helpful for input validation and consistency checks.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次