Cannot run the macro… the macro may not be available in this workbook
I am trying to call a sub on a different worksheet but I got a run time error message.
Specifically, I have two worksheets and multiple VBA sub s in those worksheets. In one of the VBA Project (say workbook1.xlsm), I have the following code:
Sub AnalysisTableMacro() Workbooks("Python solution macro.xlsm").Activate Application.Run "Python solution macro.xlsm!.PreparetheTables" End Sub
But I got the following error. The macros on both worksheets are enabled. In both worksheets, the subs are in Module1.
Cannot run the macro 'Workbook.xlsm!PrepareTheTables'. The macro may not be available in >this workbook or all macros may be disabled.
I also tried Application.Run "Python solution macro.xlsm!Module1.PreparetheTables" but did not work.
Any help would be appreciated.
Per Microsoft's KB, try allowing programmatic access to the Visual Basic project:
- Click the Microsoft Office Button, and then click Excel Options.
- Click Trust Center.
- Click Trust Center Settings.
- Click Macro Settings.
- Click to select the Trust access to the VBA project object model check box.
- Click OK to close the Excel Options dialog box.
- You may need to close and re-open excel.
Delete your name macro and build again. I did this, and the macro worked.
The most probable reason for this error is that the security feature in Excel VBA that would not allow the VBA code to be run. The user should explicitly give permission to running the excel macros alongside the workbook. This may or may not require programmatic access to the Visual Basic project.
What solved this error for me was :-
Enable Editing and Enable Content
- Changing macro settings without programmatic access Steps and details
Changing macro settings along with programmatic access(not recommended if the any of the above process works out. This would give code control to change the elements in the VBA Project itself including References and Code itself - Reference)
I had the same problem as OP and found was due to the options declaration being misspelled:
' Comment comment Options Explicit Sub someMacroMakechart()
in a sub module, instead of correct;
' Comment comment Option Explicit Sub someMacroMakechart()
If you have a space in the name of the workbook you must use single quotes (') around the file name. I have also removed the full stop.
Application.Run "'Python solution macro.xlsm'!PreparetheTables"