PDA

View Full Version : [SOLVED:] Time the duration the code is completed



almouchie
01-25-2006, 07:45 AM
I have a code that imports data from other sheets
I want to add a timer to it
so I can know how long the code is running till the code finishes

How is it doable :thumb
an example would be great

Thanks:bow:

vonpookie
01-25-2006, 08:20 AM
This is nowhere near perfect, but maybe something along these lines?


Dim StartTime As Variant, EndTime As Variant, Duration As Variant
StartTime = Now
'your code here
EndTime = Now
Duration = Format(EndTime - StartTime, "hh:mm:ss")
MsgBox "The macro ran for: " & Duration

Bob Phillips
01-25-2006, 08:45 AM
Dim nTime As Long
Dim i As Long
nTime = Timer
For i = 1 To 1000000
Debug.Print Timer - nTime & " secs"

almouchie
01-25-2006, 09:07 AM
i tried the one u provided (Vonpookie) it gave me a time of 00:00:01
which is unlikely as the macro ran for at least 5 second
how can the endtime & the start time both be Now

the second code (xls)
didnt yield any results
there is no message box in the code , how am I to find the duration
does it print it?

thanks for ur replies:bow:

vonpookie
01-25-2006, 09:27 AM
i tried the one u provided (Vonpookie) it gave me a time of 00:00:01
which is unlikely as the macro ran for at least 5 second
Well, it could seem like it ran for 5 seconds, but it's simply a matter of how long the code takes to run. Excel sometimes likes to "hang" for a couple of seconds, even though the code is onto the next line. At least that is the case for me.

how can the endtime & the start time both be Now
Because "Now" returns the current system time. When the code runs StartTime=Now, that variable saves the system time from that exact moment. Since the clock doesn't stop, when the code runs EndTime=Now, it is saving the system time from *that* particular moment.


the second code (xls)
didnt yield any results
there is no message box in the code , how am I to find the duration
does it print it?
"Debug.Print" will display the results in the immediate window in the VBE. To display the Immediate window, press Ctrl+G (or View\Immediate Window). By default it should appear at the bottom of the code pane.

If you simply want to view the results as a msgbox, replace the words Debug.Print with Msgbox

almouchie
01-25-2006, 09:50 AM
thanks a lot for ur reply & explainations
it makes perfect sense now
I used the second timer code as it displayed the seconds in more detail

ur replies are very much appreciated :)

lior03
01-25-2006, 02:35 PM
hello
i tried to use this macro.i want the second be rounded to two digits after the dot.how can i format it.


Sub baba()
Dim nTime As Long
Dim i As Long
nTime = Timer
ActiveWorkbook.Save
For i = 1 To 1000000
Next i
MsgBox "macro ran for " & Timer - nTime & " secs"
End Sub


thanks

vonpookie
01-25-2006, 02:55 PM
Try using

MsgBox "macro ran for " & Round(Timer - nTime, 2) & " secs"

lior03
01-25-2006, 03:04 PM
hello

it gave an error message.

Bob Phillips
01-25-2006, 03:07 PM
I have just noticed a small typo in my code. It causes a small error in the calculation, never greater than 1 second, but an error nonetheless.

The line


Dim nTime As Long

should be


Dim nTime As Double

lior03
02-06-2006, 03:43 AM
hello
i want the statusbar act as a clock and measure the time it take a macro to execute.
maybe:


Dim percent As Long
Application.StatusBar = Format(percent, "0.00%")
Dim nTime As Long
Dim i As Long
nTime = Timer
Dim w As Double
ActiveWorkbook.Save
w = Format(Timer - nTime, "0.00")
For i = 1 To 1000000
Next i
Application.StatusBar = "macro ran for " & w & " secs"
Application.Wait Now + TimeValue("00:00:05")
Application.StatusBar = False


xnce the mission complete i want the statusbar empty after 5 seconds
could it be doen?
thanks

Bob Phillips
02-07-2006, 10:39 AM
Doesn't that do just that?