Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Time to complete code/function

Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Time to complete code/function

try something like this...

Sub test()
Dim lng As Long
Dim dblStartTime As Double
Dim dblEndTime As Double

dblStartTime = Timer
For lng = 0 To 100000000
Next lng
dblEndTime = Timer
MsgBox "Duration " & dblEndTime - dblStartTime

End Sub
--
HTH...

Jim Thomlinson


"John" wrote:

Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Time to complete code/function

Hi John,

John wrote:
Is there a way for VBA to capture how long it takes for a piece of
code or user defined function to run? Like how fast it is on
calculating a simple UDF?


You can use the Timer function:

Dim sngTimer As Single

sngTimer = Timer

'/ your code

Debug.Print Timer - sngTimer

The resolution isn't great on the Timer function, so you may get 0 for UDFs
that don't take long to execute.

For better results, you could try the GetTickCount API function (resolution
of about 10ms):

http://www.vbforums.com/showthread.php?t=231183

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Time to complete code/function

thanks Jim I will play with this... although my first thought is... I wonder
if adding this code to code makes it slower... it must... but I can add new
code and still see the difference.

"Jim Thomlinson" wrote:

try something like this...

Sub test()
Dim lng As Long
Dim dblStartTime As Double
Dim dblEndTime As Double

dblStartTime = Timer
For lng = 0 To 100000000
Next lng
dblEndTime = Timer
MsgBox "Duration " & dblEndTime - dblStartTime

End Sub
--
HTH...

Jim Thomlinson


"John" wrote:

Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Time to complete code/function

John wrote:
Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?


:)

Declare Function GetTickCount Lib "kernel32" () As Long


Dim longStart As Long, longStop As Long
longStart = GetTickCount
......
longStop = GetTickCount


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Time to complete code/function

first, Timer is a single, so you might want to stick with declaring your
timer related variables as single.

from the immediate window (for confirmation)
? typename(timer)
Single

Second, Jim only put that loop in there as a demonstration of how to time
some sample code. You would replace that loop portion with the working
portion of your current code - the commands you want to time.

If you understood that, then my apologies - but there are many responses
that come back to suggestions such as this that indicate they don't.

--
Regards,
Tom Ogilvy




"John" wrote:

thanks Jim I will play with this... although my first thought is... I wonder
if adding this code to code makes it slower... it must... but I can add new
code and still see the difference.

"Jim Thomlinson" wrote:

try something like this...

Sub test()
Dim lng As Long
Dim dblStartTime As Double
Dim dblEndTime As Double

dblStartTime = Timer
For lng = 0 To 100000000
Next lng
dblEndTime = Timer
MsgBox "Duration " & dblEndTime - dblStartTime

End Sub
--
HTH...

Jim Thomlinson


"John" wrote:

Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Time to complete code/function

interesting, thanks one and all for the replies

"witek" wrote:

John wrote:
Is there a way for VBA to capture how long it takes for a piece of code or
user defined function to run? Like how fast it is on calculating a simple
UDF?


:)

Declare Function GetTickCount Lib "kernel32" () As Long


Dim longStart As Long, longStop As Long
longStart = GetTickCount
......
longStop = GetTickCount

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I complete increments of time in column A by say 9 min? Sandiness Excel Discussion (Misc queries) 1 December 1st 08 06:02 AM
Subtotalling taking long time to complete AndyV Excel Worksheet Functions 4 July 14th 08 03:10 PM
Reading complete Excel using DDE code amitkrsingh Excel Programming 1 August 19th 05 02:43 PM
Complete Newbe - Is this a MACRO function or VB ? Justin Excel Programming 6 July 26th 05 09:52 PM
Run time - % complete Steph[_3_] Excel Programming 3 June 25th 04 10:47 PM


All times are GMT +1. The time now is 09:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"