実務では、ファイル自動保存する際、ファイル名に年月日時分(yyyymmddhhmm)をつけて保存することがよくあったりします。今日は、Format関数を使って、年月日時分(yyyymmddhhmm)を自動取得する方法を見ていきましょう。

 

Format関数

 

構文

 

Format(値, 書式)

 

Sub test()

  ’ 月数を2桁で表示

  MsgBox Format(Month(Date), "00")

End Sub

 

実行結果

 

現在時刻から年月日時分(yyyymmddhhmm)を取得

 

Sub test2()

 

    Dim xlTime As String
    xlTime = Year(Date) & Format(Month(Date), "00") & _
                Format(Day(Date), "00") & _
                Format(Hour(Now), "00") & _
                Format(Minute(Now), "00")


    MsgBox xlTime

End Sub

 

 

 

― ― ― ― ― ― ― ― ― ― → Excel VBA基礎入門もくじ へ戻る 

 

関連記事