2017-08-04 77 views
1

我使用thespreadsheetguru.com發現這個方法來計算的話需要運行一個宏時間:VBA定時器 - 減時間等待用戶輸入

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按鈕,其他的則是帶有多個按鈕的用戶表單。

如何計算運行宏所需的時間減去等待用戶點擊按鈕的時間?

+1

*用戶輸入的多個提示* - 擺脫這些提示並收集用戶輸入,例如爲用戶輸入創建帶有手動單元格輸入的初始表。如果需要輸入中間代碼,請在每個啓動頁面之前創建多個啓動點並結束代碼,並按此方式跟蹤時間。 - >我的評論需要一點回合bc我覺得在代碼中的多個用戶提示是糟糕的用戶體驗設計。 –

回答

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 
+0

我正在考慮嘗試這樣的事情,但我希望有一個更簡單的方法。 – Robby

1

可以實現某種PauseTime的檢查?

我VBA很遠,但...

Dim arrayPausetime() as double 
Dim formStartTime as double 
Dim formStopTime as double. 

每當你啓動你需要用戶輸入的形式,你帶時間戳formStartTime,同樣當你走出這種形式...... formStopTime的。

附加(formStopTime - formStartTime)到您的arrayPausetime,並在您的宏的結尾:

dim finalTime as double 

for each (double pausetime in arrayPausetime) 
    finalTime = finalTime + (Timer - StartTime) - pausetime 

希望它可以幫助頭腦風暴! :)