-
∨目次
- 構文
- 解説
- 使用例
VBA Year 関数
はじめに
Excel VBA マクロの Year 関数を紹介します。
Year 関数は、指定した日付の年を返します。
日付から年の値を取得したいときに使用します。
- 年、月、日の値を取得するときは「Year 関数」、「Month 関数」、「Day 関数」を使用します。
- 時、分、秒の値を取得するときは「Hour 関数」、「Minute 関数」、「Second 関数」を使用します。
-
目次
- 構文
- 解説
- 使用例
構文
Year(日付)
日付から年を取得します。
引数「日付」 | 日付を指定します。 |
戻り値の型 | 数値型 (Integer) |
解説
引数「日付」が2013/1/2なら2013を返します。
引数「日付」が時刻なら、日付型の初期値1899/12/30から1899を返します。
結果を必ず 2 桁の文字列で返すなど書式を指定するには「Format 関数」を使用します。
使用例
Dim i As Integer
i = Year("2013/1/2")
Debug.Print(i) ' 2013
i = Year("2020/10/25")
Debug.Print(i) ' 2020
i = Year("1:2:3")
Debug.Print(i) ' 1899
日時の各部分を取得する。
Dim d As Date
d = "2013/1/2 3:04:05"
Debug.Print(Year(d)) ' 2013
Debug.Print(Month(d)) ' 1
Debug.Print(Day(d)) ' 2
Debug.Print(Hour(d)) ' 3
Debug.Print(Minute(d)) ' 4
Debug.Print(Second(d)) ' 5
書式を指定する。
Dim d As Date
d = "2013/1/2 3:4:5"
Debug.Print(Format(d, "yyyy")) ' 2013
Debug.Print(Format(d, "yy")) ' 13
Debug.Print(Format(d, "yyyy 年")) ' 2013 年
Debug.Print(Format(d, "yyyy/mm/dd")) ' 2013/01/02
スポンサーリンク