Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timing User 1

Status
Not open for further replies.

anastasia

Programmer
Dec 13, 2000
111
GB
Hi,

I have the following code which is activated when a user clicks on a Start button.

MsgBox "Time Started. You have 15 Minutes.", , "Time Started"

Application.OnTime When:=Now + TimeValue("00:15:00"), Name:="ProjectCompany.Module1.MacroStart"

The timer runs until 15 mins is up and then runs the code in MacroStart which is fine.

However, the user can also click on a QUIT button to exit what they are doing, and want to know how long they spent on the task until clicking on the QUIT button. e.g if they spent only 6 mins on the task. How do record the time since they hit the START button until they hit the QUIT button?, possible in minutes & seconds?.






 
Add
With Sheets("Time Store").range("A1")
.value = now()
end with

to your start macro

On your quit macro, use
EndTime = now()
StartTime = sheets("Time Store").range("A1").value
TaskTime = Format(EndTime - StartTime,"hh:mm:ss")

HTH Rgds
~Geoff~
 
Hi
very slight variation

Declare a variable StartTime in General declaration of the form module (or some other way that more than one procedure can see it. I'm not too good with declarations!!)

add the line
StartTime = Now()
to the starting process

add
MsgBox "Time used = " & Format(Now() - StartTime, "hh:mm:ss")
to the quit button code

However you will not be able to refer back to the start time again with this method unlike xlbo's

;-) If a man says something and there are no women there to hear him, is he still wrong?
 
I would take the best of both designs.

Store the data as private module level variables, but as xlbo implied, keep track of both values. It's just more convenient for you that way.

(You can also override the application's exit function to prevent people from closing the whole thing while the macro is running.)

Code:
Private StartTime As Date
Private EndTime As Date

Private Sub cmdEnd_Click()
    StartTime = Now()
End Sub

Private Sub cmdStart_Click()
    EndTime = Now()
    MsgBox "Elapsed Time: " + Format((EndTime - StartTime), "hh:mm:ss"), vbOKOnly
End Sub
 
Loomah (TechnicalUser)

Great, your code worked perfect, does exactly what I wanted.

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top