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】Timer関数|経過時間を取得する方法と注意点

Last updated at Posted at 2025-09-01

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

Timer関数の使い方と注意点

Timer関数は、現在時刻の午前0時からの経過秒数を返す関数です。
処理時間の計測や、経過時間を求める用途でよく利用されます。

構文

Timer
  • 引数 : なし
  • 戻り値 : 単精度浮動小数点数(Single型)。「午前0時からの経過秒数」で、小数点以下にミリ秒も含まれます。

使用例

現在の時刻を秒数で取得

Sub Sample()
    Debug.Print Timer
End Sub

▶ 出力結果(時刻によって変わります)

44797.86

この場合「現在時刻は午前0時から44797.86秒経過」という意味です。
→ 約 12時間26分37秒

処理時間の計測

処理開始前後の時間を保持することで処理にかかった時間を取得できます。

Sub Sample()
    Dim startTime As Single, endTime As Single
    
    startTime = Timer
    ' 何らかの処理
    Dim i As Long
    For i = 1 To 100000
        DoEvents
    Next i
    endTime = Timer
    
    Debug.Print "処理時間: " & (endTime - startTime) & "秒"
End Sub

▶ 出力結果

処理時間: 3.617188

⚠️注意

Timerの戻り値は当日0時からの経過秒

そのため、日付を跨ぐ場合はTimerだけでは正しく計算できません。
日付を跨ぐ場合はNowを使用して経過時間を計算するのが安全です。

Sub Sample()
    Dim StartTime As Date, EndTime As Date

    StartTime = Now
    ' 何らかの処理
    Dim i As Long
    For i = 1 To 100000
        DoEvents
    Next i
    EndTime = Now

    Debug.Print "経過時間: " & EndTime - StartTime & "秒"
End Sub

▶ 出力結果

処理時間: 3.617188

その他の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?