Auto run macros in Excel/VBA

Asked

Viewed 1,348 times

3

I made several attempts to execute a macro when starting Excel as indicated in the searches I did, although these indications are very simple, in my case did not work.

I followed the following script:

  1. Double click on "Esta_pasta_de_work" in the VBA Project, in "Microsoft Excel Objects"

  2. I filled in the code as below:

Public Sub Auto_Open()

    MsgBox ("Ok")

End Sub
  1. I ran the macro generated for testing and it worked.

  2. I saved the spreadsheet "macros enabled"

  3. I closed the spreadsheet and called it, the spreadsheet is open but the macro is not executed.

To test, I left a syntax error in the code, saved, closed and called again, and even the error was not shown (when executing the macro manually the error is shown), that is, the boot macro is being ignored (syntax check, just because it is not running).

I changed some of the Excel settings, such as the macros, for example, but they did not have an effect.

Before I was testing the call of a function by the Call command, calling another macro or having a form opened, and also did not work.

What is missing or what is wrong in this case?

  • 1

    Just one observation: Setting up Excel to run code automatically can lead to severe security breaches. This is not recommended on virtually any occasion, especially when speaking on a desktop, as it may allow third parties to capture and monitor computer data.

1 answer

3


I found the problem, the correct code is:

 Sub Workbook_Open()

 MsgBox ("Ok")

 End Sub

In the examples were given various names with the event "Open", in my case only worked with "Workbook_open". You can use Private Sub or Public Sub, or even Function (I read that can be used function, but not tested).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.