Format関数は日付や数値など表示形式を設定する場合に使用します。
Format関数の使用例について説明します。
Format関数の定義済みの内容などの説明は前回の内容を参考に確認お願い致します。
Format関数の構文
Format(Expression, [Format], [FirstDayOfWeek], [FirstWeekOfYear])
Format(日付や数値などの文字列, [書式指定式], [週の最初の曜日], [年内の最初の週])
[]内は省略可能です。
Format関数の使用例
・定義済みの書式で日付を表示(Long Date)
・定義済みの書式で日付を表示(Medium Date)
・定義済みの書式で日付を表示(Short Date)
・定義済みの書式で日付を表示(Long Time)
・定義済みの書式で日付を表示(Medium Time)
・定義済みの書式で日付を表示(Short Time)
・日付1桁の場合、先頭に0は付けない
・日付1桁の場合、先頭に0を付ける
・英語(省略形)表記の曜日
・日本語(省略形)表記の曜日
・英語表記の曜日
・日本語表記の曜日
・年、月、日を含むyyyy/mm/dd形式の日付を表示
・日本の場合は年月日で日付を表示
・曜日を表す数値
・その日が一年のうちで何週目かを表す数値
・月1桁の場合、先頭に0は付けない
・月1桁の場合、先頭に0を付ける
・英語(省略形)表記の月の名前
・英語表記の月の名前
・日本語表記の月の名前
・1年のうちで何番目の四半期かを表す数値
・年号の頭文字
・年号の頭文字の漢字1文字
・年号
・年号に基づく和暦1桁の場合、先頭に0は付けない
・年号に基づく和暦1桁の場合、先頭に0を付ける
・1年のうちで何日目かを表す数値
・西暦年の下2桁の数値
・西暦年の4桁の数値
・時間1桁の場合、先頭に0は付けない
・時間1桁の場合、先頭に0は付ける
・分1桁の場合、先頭に0は付けない
・分1桁の場合、先頭に0を付ける
・秒1桁の場合、先頭に0は付けない
・秒1桁の場合、先頭に0を付ける
・h:nn:ss形式の時刻
・AM/PM表記
・am/pm表記
・A/P表記
・a/p表記
・FirstDayOfWeekの設定方法
・FirstWeekOfYearの設定方法
の内容で説明をしていきます。
定義済みの書式で日付を表示(Long Date)
Sub format1()
MsgBox Format("2022/04/03 16:49:45", "Long Date")
End Sub

定義済みの書式で日付を表示(Medium Date)
Sub format2()
MsgBox Format("2022/04/03 16:49:45", "Medium Date")
End Sub

定義済みの書式で日付を表示(Short Date)
Sub format3()
MsgBox Format("2022/04/03 16:49:45", "Short Date")
End Sub

定義済みの書式で日付を表示(Long Time)
Sub format4()
MsgBox Format("2022/04/03 16:49:45", "Long Time")
End Sub

定義済みの書式で日付を表示(Medium Time)
Sub format5()
MsgBox Format("2022/04/03 16:49:45", "Medium Time")
End Sub

定義済みの書式で日付を表示(Short Time)
Sub format6()
MsgBox Format("2022/04/03 16:49:45", "Short Time")
End Sub

日付1桁の場合、先頭に0は付けない
Sub format7()
MsgBox Format("2022/04/03", "d")
End Sub

日付1桁の場合、先頭に0を付ける
Sub format8()
MsgBox Format("2022/04/03", "dd")
End Sub

英語(省略形)表記の曜日
Sub format9()
MsgBox Format("2022/04/03", "ddd")
End Sub

日本語(省略形)表記の曜日
Sub format10()
MsgBox Format("2022/04/03", "aaa")
End Sub

英語表記の曜日
Sub format11()
MsgBox Format("2022/04/03", "dddd")
End Sub

日本語表記の曜日
Sub format12()
MsgBox Format("2022/04/03", "aaaa")
End Sub

年、月、日を含むyyyy/mm/dd形式の日付を表示
Sub format13()
MsgBox Format("2022年04月03日", "ddddd")
End Sub

日本の場合は年月日で日付を表示
Sub format14()
MsgBox Format("2022/04/03", "dddddd")
End Sub

曜日を表す数値
日曜日が1、土曜日が7
Sub format15()
MsgBox Format("2022/04/03", "w")
End Sub

その日が一年のうちで何週目かを表す数値
Sub format16()
MsgBox Format("2022/04/03", "ww")
End Sub

月1桁の場合、先頭に0は付けない
Sub format17()
MsgBox Format("2022/04/03", "m")
End Sub

月1桁の場合、先頭に0を付ける
Sub format18()
MsgBox Format("2022/4/03", "mm")
End Sub

英語(省略形)表記の月の名前
Sub format19()
MsgBox Format("2022/4/03", "mmm")
End Sub

英語表記の月の名前
Sub format20()
MsgBox Format("2022/4/03", "mmmm")
End Sub

日本語表記の月の名前
Sub format21()
MsgBox Format("2022/4/03", "oooo")
End Sub

1年のうちで何番目の四半期かを表す数値
Sub format22()
MsgBox Format("2022/4/03", "q")
End Sub

年号の頭文字
Sub format23()
MsgBox Format("2022/4/03", "g")
End Sub

年号の頭文字の漢字1文字
Sub format24()
MsgBox Format("2022/4/03", "gg")
End Sub

年号
Sub format25()
MsgBox Format("2022/4/03", "ggg")
End Sub

年号に基づく和暦1桁の場合、先頭に0は付けない
Sub format26()
MsgBox Format("2022/4/03", "e")
End Sub

年号に基づく和暦 1桁の場合、先頭に0を付ける
Sub format27()
MsgBox Format("2022/04/03", "ee")
End Sub

1年のうちで何日目かを表す数値
Sub format28()
MsgBox Format("2022/04/03", "y")
End Sub

西暦年の下2桁の数値
Sub format29()
MsgBox Format("2022/04/03", "yy")
End Sub

西暦年の4桁の数値
Sub format30()
MsgBox Format("2022/04/03", "yyyy")
End Sub

時間1桁の場合、先頭に0は付けない
Sub format31()
MsgBox Format("06:49:45", "h")
End Sub

時間1桁の場合、先頭に0は付ける
Sub format32()
MsgBox Format("6:49:45", "hh")
End Sub

分1桁の場合、先頭に0は付けない
Sub format33()
MsgBox Format("06:09:45", "n")
End Sub

分1桁の場合、先頭に0を付ける
Sub format34()
MsgBox Format("06:9:45", "nn")
End Sub

秒1桁の場合、先頭に0は付けない
Sub format35()
MsgBox Format("06:49:05", "s")
End Sub

秒1桁の場合、先頭に0を付ける
Sub format36()
MsgBox Format("06:49:5", "ss")
End Sub

h:nn:ss形式の時刻
Sub format37()
MsgBox Format("06:49:5", "ttttt")
End Sub

AM/PM表記
Sub format38()
MsgBox Format("06:49:05", "AM/PM")
End Sub

am/pm表記
Sub format39()
MsgBox Format("16:49:05", "am/pm")
End Sub

A/P表記
Sub format40()
MsgBox Format("06:49:05", "A/P")
End Sub

a/p表記
Sub format41()
MsgBox Format("16:49:05", "a/p")
End Sub

FirstDayOfWeekの設定方法
日曜日が 1(規定)、土曜日が 7
通常の場合
Sub format42()
MsgBox Format("2022/04/04", "w")
End Sub

2022/04/04は月曜日のため2が表示されます。
月曜日を週の始まりとして設定
Sub format43()
MsgBox Format("2022/04/04", "w", 2)
End Sub

規定値を2(月曜日)に設定したので戻り値が1になりました。
FirstWeekOfYearの設定方法

2022年1月前半のカレンダーです。
規定として1 月 1 日が含まれる週から開始します。
Sub format44()
MsgBox Format("2022/01/2", "ww")
End Sub

2022/1/2は2週目なので、2が戻り値です。
| 定数 | 値 | 説明 |
|---|---|---|
| vbFirstJan1 | 1 | 1 月 1 日が含まれる週から開始します (既定)。 |
| vbFirstFourDays | 2 | 4 日以上が含まれる最初の週が年の第 1 週目になります。 |
| vbFirstFullWeek | 3 | 1 週間全体が含まれる最初の週がその年の第 1 週目になります。 |
FirstWeekOfYearの値を3に設定します。
Sub format45()
MsgBox Format("2022/01/2", "ww", , 3)
End Sub

1 週間全体が含まれる最初の週がその年の第 1 週目になりますので、1が戻り値です。
今回はFormat関数の日付関連の使用例について記載させて頂きました。
次回はFormat関数の数値関連について記載していきます。
少しでも皆様の参考になれば幸いです。