Home > Net >  Error 1004 Can not run the macro. The macro may not be available in this workbook or all macros may
Error 1004 Can not run the macro. The macro may not be available in this workbook or all macros may

Time:09-03

I have an Add-In installed in my Excel (which is stored in a separate folder) that I want to run in one of my Excel files through a VBA macro. Whenever I call the:

Application.Run "'Addintorun.xla'"

I get the error that the macro may not be available. I know I might need to enable all macros through Macro Security, but I can't tick this option since it's blocked by the administrator. However the add-in per se works fine, I just can't call it from the macro. I also added a reference to the Add-in in my module but nothing changed.

CodePudding user response:

There are two ways to accomplish what you need:

  1. Add the add-in as reference to the workbook you need calling its Subs, Function(s). In this way you may call them exactly as in the add-in itself. Only in case of the same macro names, the add-in name must be used when make a call.

  2. You can call the Subs which are not private in the next way:

   Application.Run "'Addintorun.xla'!MyMacro"

The simple quotes are necessary only if the add-in name contains spaces, but it is a good habit to use them all the time, since they do not bother a name without spaces, neither...

You can also obtain a result of a (not Private) function in the next way:

  Dim res As Long
  res = Application.Run("'Addintorun.xla'!MyFunction", 3, 9)
  Debug.Print res
  'MyFunction should receive two `Long` parameters and returning also a `Long`
  'It may return any kind of variable, but res must be declared to match it.

Such subs/functions can also be called using the add-inn full name, instead of its name. In this way, the add-in will be loaded/open during the call, if not already loaded...

  • Related