Hi all,
Hopefully this is an easy one. I have a main sub that runs and calls a second sub. Can I start a timer at the first sub and have it end/display when the second/called sub completes? If so can you please post a sample code? Thank you.
Hi all,
Hopefully this is an easy one. I have a main sub that runs and calls a second sub. Can I start a timer at the first sub and have it end/display when the second/called sub completes? If so can you please post a sample code? Thank you.
Have you tried Googling for "VBA Timer"? Pretty simple function to use, see here: https://www.thespreadsheetguru.com/t...macro-run-time
So you'd place the "StartTime = Timer" at the beginning of your first sub, then the "SecondsElapsed" code immediately after the call to the second sub (assuming the first sub does additional tasks after the second sub ends).
If that's not the case, you could probably use global public variables instead (stored in a standard module) and just set the start time at the start of sub1, and then just before exiting sub2, set an end time variable (or do the math to get the current time and subtract the original global variable).
No once the second sub ends that is it. So it is basically as follows:
Sub 1 (variables and start timer)
code here
Call Sub2
End Sub1
_________
Sub2
code here
End Sub2
Technically it doesn't matter that nothing else is executed in the second sub. Once the called sub (Sub2) completes, focus goes back to Sub1 to complete - in this case simply ending. You would still have the calculation done at the end of Sub1. See example below:
![]()
Please Login or Register to view this content.
Try this.
1 Create a new Class module and call it CTimer
2 Paste this code in the CTimer module.
3 To time your Sub2 try this.![]()
Please Login or Register to view this content.
![]()
Please Login or Register to view this content.
If posting code please use code tags, see here.
Thanks all. I actually went with a version of the code posted by ¯\_(ツ)_/¯. Much appreciated.
![]()
Please Login or Register to view this content.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks