EDIT: It flashes and reopens only when i got a other workbook open in the background. Just to clarify
Hi again.
When i tried that, it closed by userform, instead of disabling the timer itself hmm.
Also i got a other problem, what im trying to do is when the timer reaches 0, the book shall save and quit: Code below.
That works when the timer reaches 0, it closes perfectly, (the code is if its the only workbook active, or multiple up).
But if i try to use this code to manually save the program (with a button), it flashes, and reopens again, even i want it just to save and close (and stay closed)
The code for saving with button:
If Application.Workbooks.Count = 1 Then
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
The code i use from you, just modified, works good when the timer runs out, does what i want to:
Public ahora, g5, h5, i5
Const wTime = "00:05:00" 'time for test
Sub countdown()
i5 = h5 - g5
h5 = Time
Label1.Caption = Format(TimeValue(wTime) - TimeValue(Format(i5, "hh:mm:ss")), "hh:mm:ss")
If TimeValue(Label1.Caption) <= 0 Then
On Error Resume Next
Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
On Error GoTo 0
'MsgBox "Time's Up!"
If Application.Workbooks.Count = 1 Then
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Else
ThisWorkbook.Save
Application.DisplayAlerts = True
ThisWorkbook.Close
End If
Exit Sub
End If
Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="toReturn", Schedule:=True
End Sub
Then i tried to copy and modify the code that works, put it in a module, and made the save button to "Call mancountdown", with this code:
Const wmTime = "00:00:00" 'time for test
Sub mancountdown()
i5 = h5 - g5
h5 = Time
UserForm1.Label1.Caption = Format(TimeValue(wmTime) - TimeValue(Format(i5, "hh:mm:ss")), "hh:mm:ss")
If TimeValue(UserForm1.Label1.Caption) <= 0 Then
On Error Resume Next
Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
On Error GoTo 0
'MsgBox "Time's Up!"
If Application.Workbooks.Count = 1 Then
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Else
ThisWorkbook.Save
Application.DisplayAlerts = True
ThisWorkbook.Close
End If
Exit Sub
End If
Application.OnTime EarliestTime:=Now + TimeValue("00:00:00"), Procedure:="toReturn", Schedule:=True
End Sub
But with that, it just closes and quickly reopens again hmm.
Do you got any suggestions?
Use this
Code:
Private Sub CommandButton1_Click()
On Error Resume Next
Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
On Error GoTo 0
MsgBox "Process aborted"
End
End Sub