我使用在spreadssheetguru.com上找到的方法来计算运行宏所需的时间:
Sub CalculateRunTime_Minutes()
'PURPOSE: Determine how many minutes it took for code to completely run
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim StartTime As Double
Dim MinutesElapsed As String
'Remember time when macro starts
StartTime = Timer
'*****************************
'Insert Your Code Here...
'*****************************
'Determine how many seconds code took to run
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
'Notify user in seconds
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
End Sub
它对我来说很完美,但在我的代码中,有多个用户输入提示。有些只是OK按钮,有些是带有多个按钮的用户形式。
如何计算运行宏所需的时间,减去等待用户点击按钮所花费的时间?
答案 0 :(得分:2)
你能实施某种PauseTime检查吗?
我的VBA很远但是......
Dim arrayPausetime() as double
Dim formStartTime as double
Dim formStopTime as double.
每次启动需要用户输入的表单时,都会使用timestamp formStartTime,当你离开该表单时也是如此... formStopTime。
将(formStopTime - formStartTime)添加到您的arrayPausetime和宏的末尾:
dim finalTime as double
for each (double pausetime in arrayPausetime)
finalTime = finalTime + (Timer - StartTime) - pausetime
希望它有助于头脑风暴! :)
答案 1 :(得分:1)
基于您的代码:
Sub CalculateRunTime_Minutes()
Dim StartTime1 As Double, StartTime2 As Double, StartTime3 As Double
Dim StopTime1 As Double, StopTime2 As Double
Dim MinutesElapsed As String
StartTime1 = Timer
'your code
StopTime1 = Timer
'user input1
StartTime2 = Timer
'your code
StopTime2 = Timer
'user input2
StartTime3 = Timer
'your code
MinutesElapsed = Format(((Timer - StartTime3) + (StopTime2 - StartTime2) + (StopTime1 - StartTime1)) / 86400, "hh:mm:ss")
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
End Sub