Macro not launching on workbook open - vba

I have a workbook that I've been using an sub auto_open macro on and it's been fine.
However I am now trying to open it from another workbook and the auto_open macro doesn't work. It just opens and no macros run.
If I add a Workbook_open in "This workbook" to run the same macros it works fine. However if I run the workbook normally (outside of the link) it now opens and doesn't run any macros.
Weirdest thing is if I allow both auto_open and workbook_open it runs twice which is not what I want obviously.
Private Sub Workbook_Open()
StartMacro
End Sub
Public Sub Auto_Open()
StartMacro
End Sub
My ideal would be to have either as long as it will open when launched normally or via a link in a workbook.
Any ideas why I'm getting these issues?

auto_open subs needs to be in a module, not in an Excel object (like a sheet's code, nor ThisWorkbook). Here is further reference about auto-startup options in Excel.

Related

Error 1004 when running macro in another workbook via Application.Run

I have a number of Report Templates. Each Template calls a CommonMacroMR, which resides in the Documents - Folder. The executed code resides in ThisWorkbook Module.
When opening the report, immediately Excel comes up with
Run-time error '1004':
Cannot run the macro 'CommonMacroMR.xlsm!Workbook_Open'. The macro may not be available in this workbook or all macros may be disabled.
There is only one line of code in the Workbook_Open Sub of the report template:
Sub WorkBook_Open()
Application.Run ("CommonMacroMR.xlsm!Workbook_Open")
End Sub
In Trust - Center / Macro Settings, following Setting is activated:
Enable all macros
Trust access to the VBA project object model
Why don't you use open method for CommonMacroMR.xlsm
workbooks("CommonMacroMR.xlsm").open
workbook_open is called even if you use workbooks.open to open another file. However, you need to ensure that events are not disabled in your routines.

Workbook_Open sub won't run automatically when the workbook is opened

I need this macro to run when the .xlsm workbook is opened. It only runs if I manually run the macro, it does not start when the workbook is opened. FYI, I've checked/verified macro security (this is not signed yet).
Any ideas what I'm doing wrong?
Sub Workbook_Open()
MsgBox "Hello World!"
End Sub
Your code needs to be located in the ThisWorkbook module.
Open the VBA Editor (Alt+F11)
Open the Project Explorer (Ctrl+R)
In the Project Explorer pane, double click ThisWorkbook. (If you have multiple workbooks open, make sure you choose the ThisWorkbook under the correct project.)
In the code editor pane, click the drop-down that says General.
This will bring you to a new (or existing) Workbook_Open procedure:
Private Sub Workbook_Open()
End Sub
That's where your code should be placed.
Note that clicking the other drop-down at the top-right of the Code Editor pane, will list the other Workbook-level procedures you can add.
More Information:
Chip Pearson : Code Module And Code Names

Excel Unprotects Sheet after VBA Code Crash Despite File Not Being Saved

I have an excel book used by colleagues that is protected when they open it, when testing a new feature I was developing I have encountered an odd quirk with Excel that I don't understand:
When clicking a button linked to a Macro, the VBA code begins by unprotecting the workbook, like this:
Sub ButtonClick()
Dim userrange As Variant
Dim rrow As Range
Dim teeth As Range
' unprotect sheet
ActiveSheet.Unprotect ("password")
Application.EnableEvents = False
The macro then crashes (I know why, that's not the issue here). I then press end on the error message pop up, and close excel without saving the file. When the file is reopened, the book is unprotected.
Essentially, the code crashes before it gets here:
' protect sheet
ActiveSheet.Protect ("password")
Application.EnableEvents = True
Can I ensure the excel file is still protected when reopened, even when the VBA code crashes after unprotecting it?
The reason this is an issue is that I have some existing functionality in the workbook that only works properly when the workbook is protected. So if somebody crashes the program then tries to reopen it, they cannot use it normally again without my input.
I find it odd that Excel 'saves' the fact that the workbook was unprotected, even if I close the file without saving anything. I'm aware some 'stuff' happens in the background when running a VBA code, for example the undo stack is cleared, I'm guessing something in the background is recording the fact the worksheet has been unprotected even if I don't save the file? I'd like to understand the mechanism, if anybody has an explanation of how the protection status is recorded, it would be appreciated.
As a workaround, you could ensure the file is protected at open using the Workbook_Open event (add to ThisWorkbook module). This doesn't explain how the workbook currently remains unprotected, but you should be able to circumvent that.
Private Sub Workbook_Open()
' Ensure sheets are protected at open
ActiveSheet.Protect "password"
End Sub

AutoExec in Excel VBA

I have a Word macro (.dotm) in the Word startup folder. When it gets loaded, it automagically calls
Sub Autoexec
Call myMacro
End Sub
as explained on MSDN.
Is there a way to achieve the same effect with an Excel AddIn (.xlam) stored in the Excel startup folder?
You should use Workbook_Open as Maddy suggested in his comment, that is the recommended mtehod. Or you put in a normal module a macro which is named a little bit differently than the one in word. This also works in an AddIn
Sub auto_open()
MsgBox "Welcome"
End Sub
Also have a look for the naming convention here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-runautomacros-method-excel

Can excel macro run on browser?

Is it possible to run a macro on web browser? I can open an excel in a browser but i am not sure if macro can run with it. And also, how am I going to enable it automatically within a browser?
If I am correct in assuming that you're asking if a vba macro can be run automatically after opening it from a link on a webpage rendered by a browser, the best way to do that is to set code to run automatically whenever the workbook linked in the webpage is opened.
To have a macro run when a workbook is opened double click the "ThisWorkbook" object in the vba project explorer of the workbook that has your code and add these lines of code
Private Sub Workbook_Open()
Call Module1.hello 'Change this line to start your code
End Sub
This automatically executes the sub "hello" in Module1 of the workbook.
Note that the user has to have Macros Enabled in their Trust Center Settings for anything to run.
Also note that depending on their browser and where the actual link points to the workbook file might have to be downloaded first and then opened from the download location for the macro to run.