メソッドで開始/終了したい場合(クラス版・任意の場所でStop)
CBlockTimer.cls
Option Explicit
Private t0 As Double, lbl As String, active As Boolean
Public Sub Start(Optional ByVal label As String = "", Optional ByVal autoLog As Boolean = True)
lbl = label: t0 = NowHiRes(): active = autoLog Or True
End Sub
Public Function [Stop](Optional ByVal overrideLabel As String = "") As Double
If Not active Then Exit Function
Dim ms As Double, l As String
ms = (NowHiRes() - t0) * 1000#
l = IIf(overrideLabel <> "", overrideLabel, lbl)
Debug.Print "⏱ " & IIf(l <> "", l & ": ", "") & Format(ms, "0.000") & " ms"
[Stop] = ms: active = False
End Function
呼び出し側.vba
Sub Example_Class()
Dim m As New CBlockTimer
m.Start "集計"
' ... 処理 ...
Dim ms As Double: ms = m.Stop() ' 任意の地点で終了
End Sub