Close automatically a userform

G

Guest

How to close a userform if no entry is made within 10 seconds? Kindly help
with an appropriate macro.
 
G

Guest

Code goes into the user form's code section, not the workbook itself. For
the examples, the user form's name is UserForm1

1st method: if no entry made for 10 seconds, form closes, but any entry made
causes it to remain open no matter how long it takes to finish entries.

Option Explicit
Public myUserFormFlag As Boolean

Private Sub UserForm_Activate()
Dim startTimer As Long
myUserFormFlag = False
startTimer = Timer
Do While myUserFormFlag = False
DoEvents
If Timer > (startTimer + 10) Then
UserForm1.Hide
Exit Do
End If
Loop
End Sub

Private Sub ComboBox1_Change()
myUserFormFlag = True
End Sub

Private Sub TextBox1_Change()
myUserFormFlag = True
End Sub

The 'secret' in that is that you need to add the line
myUserFormFlag = True
at the start of any action event for any of your controls on the form. Then
when any one of them is used, the timing tests stop and the form stays open.

A second method: this one will keep the form open as long as someone does
something, as type a letter into a text box, at least once each 10 seconds,
but once no action has taken place on the form at all for 10 seconds, it
closes, even with partially completed text entries:

Option Explicit
Public startTimer As Long

Private Sub UserForm_Activate()
startTimer = Timer
Do While Timer < (startTimer + 10)
DoEvents
Loop
UserForm1.Hide
End Sub

Private Sub ComboBox1_Change()
startTimer = Timer
End Sub

Private Sub TextBox1_Change()
startTimer = Timer
End Sub

With this second one, the 'secret' is in resetting startTimer when any
control is changed/used on the form. So the 10-second clock gets restarted
any time there is activity on the form.

I showed how to do it with a form with a ComboBox and a TextBox on it, just
do much the same for any/all controls on the form and you should be fine.
 
G

Guest

Thanx bro
Shall try out the same.
Can u help me out one another problem?
I want a timestamp to be appearing for each and every entry in column A in
corresponding column B. The problem is that whether the entry is made as
punched/typed or pased. In other words if I paste an array/range in a single
Ctrl+V or Enter stroke, on cells A1:A10 I want the cells B1:B10 to be
reflecting a time stamp on every cell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top