構文
Year 年のみ取得
Month 月のみ取得
Day 日のみ取得
指定した値から年、月、日を分けて取得します。
戻り値は全て数値型(Integer)です。
戻り値は全て数値型(Integer)です。
Year/Month/Day関数のサンプルコード
Public Sub sample_Year_Month_Day() Dim str As String: str = "2021/02/22 12:01:30" '■通常の使い方 Debug.Print Year(str) '2021 Debug.Print Month(str) '2 Debug.Print Day(str) '22 '■Format関数でも同様結果取得可能 Debug.Print Format(str, "yyyy") '2021 Debug.Print Format(str, "m") '2 Debug.Print Format(str, "d") '22 '■時刻データのみの場合は1899/12/30と日付型の初期値を取得 str = "12:01:30" Debug.Print Year(str) '1899 Debug.Print Month(str) '12 Debug.Print Day(str) '30 '■Date型のデータ以外はエラー '実行時エラー13 型が一致しません Debug.Print Year("あいうえお") Debug.Print Month("abc") Debug.Print Day("") End Sub
注意点
- Format関数で同様の結果を取得できるので、必ずしも覚える必要はありません。
- 日付データ以外はエラーとなるので、日付かどうか判断する場合はisDate関数を使用します。
関連記事
日付データかどうか判断する【IsDate関数】【ExcelVBA】
時刻データかどうか判断する【IsDate関数応用】【ExcelVBA】
文字列を日付データに変換する【CDate関数】【ExcelVBA】
年/月/日から日付データを取得する【DateSerial関数】【ExcelVBA】
翌月末日や翌20日を簡単に求めたい(DateSerial関数)
西暦→和暦変換-和暦→西暦変換【DATEVALUE関数】【ExcelVBA】
コメント