- Joined
- Oct 5, 2001
- Messages
- 30,080
The aim of this thread is to include code demonstrating "how to" do various things in VB (or VBA at a stretch). People who know how to program in VB, please contribute (code snippets and pointing out bugs. When pointing out bugs, please do it via PM).
I suggest that short code snippets be posted as text, longer ones posted as attachments.
If anyone has any "how do I ...." type questions, please post them.
And as a starter:
How do I time how long it takes to run code?
I suggest that short code snippets be posted as text, longer ones posted as attachments.
If anyone has any "how do I ...." type questions, please post them.
And as a starter:
How do I time how long it takes to run code?
This code will (usually) give you millisecond resultion (you can actually interrogate windows to find the actual resolution, but that can be the subject of further code!There are two (easy) methods. Firstly, you can use the VB "Timer" function.
Dim sgl_StartTime as single
Dim sgl_EndTime as single
sgl_StartTime = Timer
'------------------------------
'Code to time goes here
'------------------------------
sgl_EndTime = Timer
msgbox "Time taken = " & sgl_EndTime - sgl_StartTime & " Seconds."
This will only give you resolution to the nearest second, which is often not that useful for code optimisation. A "better" method is to use a Windows timer function. You will need to declare this function to use it:
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Dim lng_StartTime as Long
Dim lng_EndTime as Long
lng_StartTime = TimeGetTime
'------------------------------
'Code to time goes here
'------------------------------
lng_EndTime = TimeGetTime
msgbox "Time taken = " & lng_EndTime - lng_StartTime & " milliseconds."