Link to home
Start Free TrialLog in
Avatar of ragnarok89
ragnarok89

asked on

Excel VBA Auto_Open Macro

Hi all, I have what I think should be a simple question, but I can't get this to work. I have an excel file that uses the Auto_Open sub so that code is run UNATTENDED whenever the file is opened (scheduled task). However, sometimes I would like to open the file manually and prevent this code from running. Here's my macro:
Sub Auto_Open()
   UserForm1.Show
   MsgBox("Auto_Open completed")
end sub

Open in new window


My form has one button only. Here's the code and properties:
Private Sub CommandButton1_Click()
    UserForm1.Hide
    End
End Sub

Private Sub UserForm_activate()
    Application.Wait Now + TimeValue("0:00:05")
    UserForm1.Hide
End Sub

Open in new window

User generated image
So what SHOULD happen is:
1) File opens
2) Autorun macro starts
3) Userform1 is opened
4a) if user does nothing, after 5 seconds userfrom1 closes, and then
      the "Auto_Open completed" msg is displayed
4b) however, if user clicks button, ALL macros should stop; i.e. user
      should NOT see  "Auto_Open completed" msg

What actually happens is:
1) File opens
2) Autorun macro starts
3) Userform1 is opened
4) I cannot click the button, so the "Auto_Open completed" msg is always displayed

 I am doing this so I don't have to constantly toggle my macro security settings. Where am I going wrong?
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ragnarok89
ragnarok89

ASKER

dlmille,

I see what you're trying to do... However, when I run it, I get 2 errors. First, I think "Public stopAutomation As Boolean" should be "Dim stopAutomation As Boolean" I made this change and got this:

"Wrong number of arguments or invalid property assignment" this error occurs on the line

Application.Wait Now + TimeValue("0:00:05"), "continueAutomation"

I believe it doesn't like the        , "continueAutomation"
ok...

Application.OnTime Now + TimeValue("0:00:05"), "continueAutomation"

works, but the complete macro always runs, even if I click the button on the form...
OK...

"Public stopAutomation As Boolean" is in fact correct. I understand that part now
continueAutomation (and you can change the name) is just the name of a public macro that you want to run if the user doesn't interrupt the process by clicking Cancel which sets the public variable stopAutomation.