Start countdown when form opens and close form when complete

Wilfred_E

New Member
Joined
May 9, 2019
Messages
44
I need a VBA code to auto-start and show countdown moment form opens and closes when countdown completes. I would like other form contents unfreezed (clickable and working) while countdown is counting. I have tried the following but ain't working:

VBA Code:
Private Sub UserForm_Activate()

Application.OnTime (Now + TimeValue("00:00:01")), "UserForm_Activate"
Question1.Label1.Caption = "3"
Question1.Repaint

Application.OnTime (Now + TimeValue("00:00:01")), "UserForm_Activate"
Question1.Label1.Caption = "2"
Question1.Repaint

Application.OnTime (Now + TimeValue("00:00:01")), "UserForm_Activate"
Question1.Label1.Caption = "1"
Question1.Repaint
Unload Me

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:

In userform code:
VBA Code:
Private Sub UserForm_Terminate()
  Call CountDown_End
End Sub

Private Sub UserForm_Initialize()
  Label1.Caption = 10 'Write the starting number of the count.
  Call CountDown
End Sub

In a Module:
VBA Code:
Sub CountDown()
  If Val(Question1.Label1.Caption) = 0 Then
    Unload Question1
    Exit Sub
  End If
  Question1.Label1.Caption = Val(Question1.Label1.Caption) - 1
  DoEvents
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub

Sub CountDown_End()
  On Error Resume Next
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Sub
 
Upvote 0
Try this:

In userform code:
VBA Code:
Private Sub UserForm_Terminate()
  Call CountDown_End
End Sub

Private Sub UserForm_Initialize()
  Label1.Caption = 10 'Write the starting number of the count.
  Call CountDown
End Sub

In a Module:
VBA Code:
Sub CountDown()
  If Val(Question1.Label1.Caption) = 0 Then
    Unload Question1
    Exit Sub
  End If
  Question1.Label1.Caption = Val(Question1.Label1.Caption) - 1
  DoEvents
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub

Sub CountDown_End()
  On Error Resume Next
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Sub
Thank you very much DanteAmor. This works perfectly and meets my need. However, I realized it would be better the countdown appear in a time format i.e. countdown from 00:00:10 to 00:00:00. Please, is this possible to achieve? Thanks
 
Upvote 0
Change code for this:

VBA Code:
Private Sub UserForm_Terminate()
  Call CountDown_End
End Sub

Private Sub UserForm_Initialize()
  Label1.Caption = "00:00:10" 'Write the starting number of the count.
  Call CountDown
End Sub

VBA Code:
Sub CountDown()
  If Question1.Label1.Caption = "00:00:00" Then
    Unload Question1
    Exit Sub
  End If
  Question1.Label1.Caption = Format(TimeValue(Question1.Label1.Caption) - TimeValue("00:00:01"), "hh:mm:ss")
  DoEvents
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub

Sub CountDown_End()
  On Error Resume Next
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Change code for this:

VBA Code:
Private Sub UserForm_Terminate()
  Call CountDown_End
End Sub

Private Sub UserForm_Initialize()
  Label1.Caption = "00:00:10" 'Write the starting number of the count.
  Call CountDown
End Sub

VBA Code:
Sub CountDown()
  If Question1.Label1.Caption = "00:00:00" Then
    Unload Question1
    Exit Sub
  End If
  Question1.Label1.Caption = Format(TimeValue(Question1.Label1.Caption) - TimeValue("00:00:01"), "hh:mm:ss")
  DoEvents
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown"
End Sub

Sub CountDown_End()
  On Error Resume Next
  Application.OnTime (Now + TimeValue("00:00:01")), "CountDown", , False
End Sub
Hi DanteAmor, I really truly appreciate your assistance yesterday. However, am afraid, I would need few things added: "I would Iike the given countdown and close code to submit the selected OptionButton's value to a specified range then save and close the workbook and close form finally. I was able to use CommandButton to submit the selected value but needs and couldn't fix the countdown snipet to do same. NB: I have 4 OptionButtons wrapped in a frame. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top