PDA

View Full Version : Solved: TIMER



krishhi
01-31-2010, 10:17 PM
Hey guys,

Can anyone has idea to set a timer like from 60 seconds to 0 seconds then the form automatically closed.

Any help?

krrish

RolfJ
02-01-2010, 12:48 AM
This simple example is intended to give you an idea how to do this. The code needs to be placed in a standard VBA module and your project needs to include a form named UserForm1.


Sub ShowFormAndCloseAfter60Seconds()
Application.OnTime Now + TimeValue("00:00:60"), "CloseForm"
UserForm1.Show
End Sub

Private Sub CloseForm()
Unload UserForm1
End Sub

krishhi
02-01-2010, 05:41 AM
This simple example is intended to give you an idea how to do this. The code needs to be placed in a standard VBA module and your project needs to include a form named UserForm1.


Sub ShowFormAndCloseAfter60Seconds()
Application.OnTime Now + TimeValue("00:00:60"), "CloseForm"
UserForm1.Show
End Sub

Private Sub CloseForm()
Unload UserForm1
End Sub



thanks for the reply, but i need some text showing the count down on the form.

RolfJ
02-01-2010, 07:46 AM
Well, that's different. Add a label (Label1) to UserForm1. Then add the following code to the VBA module of that form:


Const MAX_SECONDS As Integer = 60

Private Sub UserForm_Activate()
Dim t As Double
Dim secToGo As Integer
secToGo = MAX_SECONDS
t = Timer
Do
If Timer - t > 1 Then
t = Timer
secToGo = secToGo - 1
If secToGo = -1 Then Unload UserForm1
Label1.Caption = secToGo
Me.Repaint
End If
DoEvents
Loop
End Sub

Private Sub UserForm_Initialize()
Label1.Caption = MAX_SECONDS
End Sub

Private Sub UserForm_Terminate()
End
End Sub



and this code to a standard VBA module:


Sub ShowForm()
UserForm1.Show
End Sub

krishhi
02-01-2010, 09:05 AM
Thank you very much.
but there is nothing happened. If you don't mind can u provide a workbook of this same.

mdmackillop
02-01-2010, 03:23 PM
A little rough but try this

RolfJ
02-01-2010, 07:40 PM
After copy the code into the prescribed VBA modules you need to actually start the macro, e.g. by selecting, from the Main Menu in Excel:

Tools | Macro | Macros

then highlight 'ShowForm' and click on 'Run'. Alternatively, in case you can download it, click on the button I placed on Sheet1 of the attached workbook following mdmackillop's approach.

krishhi
02-02-2010, 07:51 PM
Wow thank you so much.