0

I have a code to set a timer of 5 minutes on my userform. It works when I press the command button.

How do I make it run automatically at the start ? I tried in ThisWorkbook but it didn't worked.

here's the code :

In a module:

Public Const AllowedTime As Double = 1

In the Userform:

Private Sub CommandButton1_Click()

Dim userClickedPause As Boolean ' Gets set to True by the Pause button

Dim stopTime As Date

    userClickedPause = False
    ' If AllowedTime is the number of minutes with a decimal part:
    stopTime = DateAdd("s", Int(AllowedTime * 600), Now) ' add seconds to current time

    ' If AllowedTime is the number of seconds:
    'stopTime = DateAdd("s", AllowedTime, Now) ' add seconds to current time
    Do
        With UserForm1.TextBox1
            .Value = Format(stopTime - Now, "Nn:Ss")
        End With
        DoEvents
        If userClickedPause = True Then
            Exit Do
        End If
    Loop Until Now >= stopTime


End Sub
Private Sub CommandButton2_Click()
    userClickedPause = True
End Sub
5
  • Where is the code for the Timer event? Commented Dec 8, 2019 at 20:26
  • there's none. @braX I also wont need the pause function of this code. I just wanna see 10 minutes go by. Commented Dec 8, 2019 at 20:29
  • Well that's why it's "not working" then. Commented Dec 8, 2019 at 20:30
  • @braX I dont know what and what part of the code to place in ThisWorkbook to have it run automatically. For now, I have to click on a command button to run 10 minutes to 0. I would like to have it run once I open the workbook. This is what I dont know how. I pasted the code CommandButton1_Click() in ThisWorkbook but nothings happening. Commented Dec 8, 2019 at 20:32
  • Did you put it in the right event? or did you just paste it in randomly with no specific name? learn.microsoft.com/en-us/office/vba/api/excel.workbook.open Commented Dec 8, 2019 at 20:33

1 Answer 1

1

I would recommend you to re-structure your code. Since you want to call your StartTimer sub from both a UserForm and the Workbook_Open event, put it into a module.

Then you simply call the sub from your Commandbutton1_Click event and the Workbook_Open event.

'Put this code inside ThisWorkbook
Private Sub Workbook_Open()
    'This sub into ThisWorkbook
    Application.Run "SetTimer"
End Sub

'This sub goes into a module
Private Sub SetTimer()
    'Here goes your code that sets the timer
    '(move it from your UserForm)
    '.......................................
    '.......................................
End Sub

'The code inside your UserForm
Private Sub CommandButton1_Click()
    Application.Run "SetTimer"
End Sub
Sign up to request clarification or add additional context in comments.

14 Comments

@Codingnoob Check if "Events" are enabled. You can type ?Application.EnableEvents into the "Immediate Window" inside the VBE. If it returns FALSE, you can type Application.EnableEvents=True to enable events.
Well, if you have your code inside ThisWorkbook in the Workbook_Open() Sub and Events enables, the only further thing that comes into my mind would be security settings that prevent the macro from running when the workbook opens. But I assume you checked those?
Have you tried it? It doenst work at all. Also you ommited to tell me where do I insert Public Const AllowedTime As Double = 1. I copied it inside another module, it still only works when I click the command button. It doesn't run automatically.
The Public declaration can be in any module (but not in the UserForm, Sheet or ThisWorkbook). And no, I have not tried it, I showed you the general approach - which works if done right.
Good to hear. Not sure if that can be fixed anyway. Probably the same problem like Excel blanking out while a heavy macro is running.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.