Calling Macro From Workbook
Calling Macro From Workbook
3
I am trying to call a Sub I defined inside a sheet from a module.
7
You need to set your function to Public as shown below
Sheet1:
Sub callTest()
With Sheet1
Call .test
End With
End Sub
shareimprove this answer
answered Nov 20 '15 at 16:51
Estevam Garcia
339217
add a comment
2
If it's in the same project and of Public scope - just use the name of the sub.
If it's in a another workbook, use Application.Run()
1.
Sub Foo()
Bar '// Runs Sub Bar()
End Sub
Sub Bar()
MsgBox "I got called!"
End Sub
2.
Sub Foo()
Application.Run "MyOtherWorkbook.xlsm!Bar"
End Sub
shareimprove this answer
answered Nov 20 '15 at 16:43
Sam
15.7k33055
add a comment
2
From any module or sheet (that has a code), if you have a sub called Button1_click() in "Sheet12"
and you want to call it from other module or sheet. just type
call Sheets("Sheet12").Button1_click 'Without parenthesis
shareimprove this answer
answered Oct 26 '17 at 13:28
Mohammad ElNesr
88611227
add a comment
1
for me, I had to change the Sheet Sub Procedure from Private to Public for the Call to work.
Call Worksheets("Sheet_name").Sub_name
In my case:
Call YourMacroName
You do not have to use Call but I think it is clearer when you read the code that
another macro is called.
But what if you want to run a macro that is in another workbook or Add-In(File or
add-in must be open).
We can use Application.Run if we want that like this :
Application.Run "Book1.xls!MyMacroName"
It does not do any harm to use the single quotes even if not needed, and always
include them if the workbook name is not known in advance, for example if the
workbook name is a variable like this
Application.Run "'" & strFileName & "'!MyMacroName"
Note: If your workbook name contains apostrophe (') characters, such as in "Joe's
Workbook.xls", then you need to double-up the apostrophes like Application.Run
"'Joe''s Workbook'!MyMacroName"
But what if you use Excel 2007-2016 and use custom Ribbon controls with
callbacks.
Sub TestMe()
MsgBox "Hi there"
End Sub
You will notice that the Application.Run examples above will not work when you
want to run a callback in another workbook or Add-in. Also Call
MyMacroName will not work to call a callback in the same workbook.
Sub test1()
Dim obj As Object
TestMe obj
End Sub
Sub test2()
Dim obj As Object
Application.Run "'RDBMerge.xlam'!RunRDBMergeForm", obj
End Sub
Note: Instead of a object you can also use IRibbonControl like this :
Sub test3()
Dim IRCdummy As IRibbonControl
TestMe IRCdummy
End Sub
Sub ErrorTest()
Dim TestWkbk As Workbook
Dim obj As Object
End If
End Sub
Tip: You could replace the MsgBox that says that the file is not open with code that
opens the workbook/add-in. Set TestWkbk =
Workbooks.Open("C:\YourPathToTheAddin\RDBTestAdd-in.xlam")
Do not forget to check if opening the file was succesful in the code before you try to
call the macro or callback.
Another way to test if a workbook/add-in is open is to call a function like this with
as argument the workbook name that you want to check.
But how do we use this function if it is not in the same workbook, you can use this
to check if there is a file named RDBMerge.xlam open.
Acknowledgements
Many thanks to Peter Thornton, Jim Rech, Dave Peterson and Mike Rosenblum for
their useful comments.