Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running a macro from an add-in

Tags:

excel

vba

i have an add-in called book1. inside the addin there is a module called module1 which has a sub called addin1

i would like to run the macro addin1 from a different workbook

i am trying to call this macro like this:

Call Addin1

but that's not working

and i tried:

Call book1.xlam.Module1.AddIn1

which is not working either

does anyone know how to run a macro that is within an add-in ?

like image 411
Alex Gordon Avatar asked May 11 '10 20:05

Alex Gordon


People also ask

How do I run an addin in VBA?

Make sure that you click on the workbook you want to add the reference to, and from the VBA editor menu choose Tools -> References. In the displayed list check the box beside your renamed add-in, and then click on OK. You'll see that your workbook now has a new reference to the add-in.

How do you get macro Add-in in Excel?

Click the File tab, click Options, and then click the Add-Ins category. In the Manage box, click Excel Add-ins, and then click Go. The Add-Ins dialog box appears. In the Add-Ins available box, select the check box next to the add-in that you want to activate, and then click OK.

How can you run a macro from inside the code editor?

Step - This will open the Visual Basic Editor to the first line of the macro. Pressing F8 will let you step through the macro code one line at a time. Edit - This will open the Visual Basic Editor and let you edit the macro code as needed. Once you've made changes, you can press F5 to run the macro from the editor.

Can I run one macro from another?

VBA Example: Run Another Macro from a Macro Just type the word Call then space, then type the name of the macro to be called (run). The example below shows how to call Macro2 from Macro1. It's important to note that the two macros DO NOT run at the same time.


3 Answers

(Assuming Office 2007) Here's an example:

1- Open a new Workbook.
2- Add a macro that contains the code MsgBox("Add-In")
3- Save as xlam file.
4- Open new Workbook.
5- Click Office Button -> Excel Options
6- Click the Add-Ins tab on the left.
7- At the bottom, next to the "Manage" dropdown, select "Excel Add-Ins" and click Go.
8- Click Browse and navigate to your xlam file.
9- Ensure the box next to your file is checked and click Ok.
10- Click the Office Button -> Excel Options.
11- Click Customize Tab.
12- Select Macros in the "Choose Commands From Dropdown."
13- Double click your AddIn and now a button will appear on the Quick Access Toolbar.
14- Click the button and your message box will now show.

like image 56
Jacob G Avatar answered Oct 15 '22 08:10

Jacob G


In your workbook you write:

Sub test() 
    ' from other excel file
    Application.Run ("youraddin.xla!ShowForm") 
End Sub

And in the addin you have

Public Sub ShowForm() 
    loginform.Show 
End Sub 
like image 8
nathanvda Avatar answered Oct 15 '22 09:10

nathanvda


In relation to how to run subroutines in installed add-ins (.xlam):

  1. Make sure the VBA project associated with the add-in has a unique name (not VBAProject) - say Addin_1.

  2. Within the VBA project for the workbook from which you wish to call the subroutines in the add-in, set a reference to Addin_1. Tools > References, find Addin_1 in the list of available references, and click on the adjacent box (a tick appears).

  3. Within a subroutine in a code module of your workbook, you can now call a subroutine of the add-in, using:

      Call Addin_1.routine_name(routine parameters)
    

Since you have a reference to Addin_1, the names of the subroutines in the add-in will appear once you type Call Addin_1. and the parameter list for the specific subroutine will appear once you have typed Call Addin_1.routine_name(, which greatly assists in writing error-free code.

Tested with Excel 2013. Discovered when I was curious to see what happened when I set a reference to the VBA project of an installed add-in (.xlam). I have not seen this documented.

like image 4
RAK_da_Pira Avatar answered Oct 15 '22 09:10

RAK_da_Pira