29
votes

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.

13

13 Answers

21
votes

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"
7
votes

Had the same issue and I 'Compiled VBA Project' which identified an error. After correction and compiling, the macros worked.

7
votes

You also run into this issue when you are creating routine in a class module.

When you try to run the code externally, you get this error.
You can't assign macro to button to a member of a class module either.

If you try to run from within the code by pressing green play button you will also see the same error.

Either move the routine in to a regular module or create a new routine in a regular module that calls the class member.

7
votes

In my case this error came up when the Sub name was identical to the Module name.

6
votes

Per Microsoft's KB, try allowing programmatic access to the Visual Basic project:

  1. Click the Microsoft Office Button, and then click Excel Options.
  2. Click Trust Center.
  3. Click Trust Center Settings.
  4. Click Macro Settings.
  5. Click to select the Trust access to the VBA project object model check box.
  6. Click OK to close the Excel Options dialog box.
  7. You may need to close and re-open excel.
2
votes

In my case the error happened when I placed my macro (public sub) into a ThisWorkbook section of the file expecting it will make it visible for Application.Run function. This was not the case and I got that error you mentioned.

I moved my macro into a separate Module and it resolved the problem.

2
votes

I had a problem with this error as well, turned out the the filename was causing the problem.

I was calling it like this: Application.Run "'" & strPath & strFName & "'!UPC.PrintaFew"

The variable strFName contained an apostrophe in it which, of course messed things up. Took me hours to figure it out. But once the apostrophe was removed from the filename it worked.

1
votes

I had to remove all dashes and underscores from file names and macro names, make sure that macro were enabled and add them module name.macro name

This is what I ended up with: Application.Run ("'" & WbName & "'" & "!ModuleName.MacroName")

1
votes

Go into task manager and see if you have any Microsoft Excel Processes running in the background. I closed my excel background processes and my code worked again.

0
votes

Delete your name macro and build again. I did this, and the macro worked.

0
votes

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()
0
votes

This error also occurs if you create a sub or function in a 'Microsoft Excel Object' (like Sheet1, Sheet2, ...) instead to create it in a Module.

For example: you create with VBA a button and set .OnAction = 'btn_action' . And Sub btn_action you placed into the Sheet object instead into a Module.

0
votes

I had the same error message "Cannot find the macro ___ etc" and this problem persisted even after I'd saved the file as an XLSX. This seemed very odd ... how could it be running a macro when the file has no macros!

When I examined the file using the Custom UI Editor, I found that a macro was being called when the file was initialized and another custom macro was being called when the user clicked the Save button.

Using the Custom UI Editor, I removed all the XML code and the error messages disappeared.