0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Excel VBA】Minute関数|時刻から「分」を取り出す方法と注意点

0
Last updated at Posted at 2025-07-14

この記事ではMinute関数の使い方と注意点について解説します。
他のよく使うVBA関数一覧はこちら。

Minute関数の使い方と注意点

Minute関数は、指定した時刻から「分」の部分だけを取得する関数です。
0〜59の整数を返します。

構文

Minute(時刻)
  • 時刻 : 時刻を表すDate型の値、または時刻として解釈可能な文字列
  • 戻り値 : 指定した時刻の「分」(Integer型、0〜59)

使用例

時刻から「分」を取得する

Sub Sample()
    Dim MyTime As Date
    MyTime = #15:45:12#
    Debug.Print Minute(MyTime)
End Sub

▶ 出力結果

45

Now関数の結果から分を取得する

Sub Sample()
    Debug.Print Minute(Now)
End Sub

▶ 出力結果(実行が15時45分の場合)

45

分の値で処理を分ける

Sub Sample()
    Dim MyMinute As Integer
    MyMinute = Minute(Time)

    If MyMinute < 30 Then
        Debug.Print "前半の30分"
    Else
        Debug.Print "後半の30分"
    End If
End Sub

▶ 出力結果(実行が15時45分の場合)

後半の30

⚠️注意

文字列を渡す場合は、時刻として解釈できる形式にする必要がある

Sub Sample()
    On Error Resume Next

    Err.Clear
    Debug.Print Minute("15/45/12")    ' 区切り文字が不正
    If Err.Number <> 0 Then Debug.Print "15/45/12 : エラー:" & Err.Description

    Err.Clear
    Debug.Print Minute("15:60:00")    ' 60分は存在しない
    If Err.Number <> 0 Then Debug.Print "15:60:00 : エラー:" & Err.Description

    Err.Clear
    Debug.Print Minute("一分後")      ' 自然言語は無効
    If Err.Number <> 0 Then Debug.Print "一分後 : エラー:" & Err.Description

    On Error GoTo 0
End Sub

▶ 出力結果

15/45/12 : エラー:型が一致しません。
15:60:00 : エラー:型が一致しません。
一分後 : エラー:型が一致しません。

上記ではエラー発生時にエラーを無視して次の処理を実行するよう記述していますが、
エラーを無視する記述が無い場合、エラー時に処理が止まってしまいます。
引数に渡す内容には注意しましょう。

戻り値に「前ゼロ」は付かない

戻り値は数値であり文字列ではないため前ゼロ(例:01)は付きません。
前ゼロを付けたい場合にはFormat関数を使用しましょう。

Sub Sample()
    Debug.Print Format(Minute(Now), "00")
End Sub

▶ 出力結果(実行が5分の場合)

05

関連するVBA関数

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?