PDA

View Full Version : Macro to Deduct and Display Time in Userform TextBox



Silver
11-10-2016, 09:06 PM
Hi,

I came across a code that displays current time in userform textbox.

Below is the code -

Userform Code


Option Explicit

Private Sub CommandButton1_Click()
Application.Run "StopTimer"
Unload Me
End Sub


Private Sub UserForm_Activate()
Application.Run "StartTimer"
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
StopTimer
End Sub

In a Module


Option ExplicitDim T


Sub StopTimer()
On Error Resume Next
Application.OnTime T, Procedure:="Update", Schedule:=False
End Sub


Sub StartTimer()
T = Now + TimeValue("00:00:01")
Application.OnTime T, "Update"
End Sub


Sub Update()
UserForm1.TextBox1.Value = Format(Now, "hh:mm:ss")
Call StartTimer
End Sub

How do I get the code to deduct 05 Hrs and 30 Mins and then display the time.

Have attached test workbook for reference.

Any assistance will be appreciated.

mana
11-11-2016, 04:31 AM
???



Sub test()

MsgBox Format(Now, "hh:mm:ss") & vbCrLf & _
Format(Now - TimeSerial(5, 30, 0), "hh:mm:ss")

End Sub

Silver
11-11-2016, 06:33 AM
Hi mana,

Thanks for responding.

The code provided by you is a timestamp. However, it did help me out.

Took part of your code and added it to mine and achieved what I was looking for.

I changed below code -



Sub Update()
UserForm1.TextBox1.Value = Format(Now, "hh:mm:ss")
Call StartTimer
End Sub

To -



Sub Update()
UserForm1.TextBox1.Value = Format(Now - TimeSerial(5,30,0), "hh:mm:ss")
Call StartTimer
End Sub

Thank you...:thumb :yes