これはデートですか? という意味深な質問を投げかけるIsDate関数のご紹介です。真、偽を返すので条件分岐に使いやすく、フラグを容易に立てることができます。
IsDate関数の使い方
引数が日付かを判定
IsDate(引数)
IsDate(引数)

とりあえず漢数字で日付を書きました。このA1セルを日付判断ボックスとします。

VBAを開き、マクロを書いていきます。
まずIf文でIsDate(Range(“A1”).Value)を判定します。日付ならTrueが返り、そうでないならFalseが返るので、これ以上の記述は不要です。
まずIf文でIsDate(Range(“A1”).Value)を判定します。日付ならTrueが返り、そうでないならFalseが返るので、これ以上の記述は不要です。

日付であればそうである旨、

ちがうならちがう旨、メッセージボックスで表示します。

実行結果。一月二日と普通に書いたものは文字列とされ、日付認定されませんでした。

Excelが日付認定するデータは、たとえばこういう感じのものです。

自動で書式が日付になり、表記が変更されました。これは当然パスします。

書式を変更しシリアル値にすると、これは数値として扱われます。パスしません。

なお、存在しない日付でもそれっぽければパスします。「今月何日までだっけ?」的な使い方はできません。

逆に明らかに日付でないものを書式だけ日付にしても……。

これはパスしません。
' vba
If IsDate(Range("A1").Value) Then
MsgBox "日付です"
Else
MsgBox "日付ではないです"
End If
Next
IsDate関数の使い方については以上です。同様に数値を判定する場合はIsNumeric関数、空白かどうかはIsEmpty関数の記事をご参照ください。
文字列を判定したい、あるいはTrue/Falseではなくデータ型の名称を取得したい・条件に使いたい場合は、TypeName関数を使います。

やりたいことから方法を探すエクセル(Excel)操作・関数・VBA(マクロ)逆引きまとめ
逆引き(やりたいことから探す)Excel記事まとめ

関数・演算子・メソッド・プロパティ名から探すExcel/VBA(マクロ)使い方・組み合わせ方まとめ
こちらはExcelやメソッドの諸機能を、機能の名称から探せるまとめ記事です。
コメント