0% found this document useful (0 votes)
70 views5 pages

Calling Macro From Workbook

This document discusses different ways to call subs and functions between worksheets and modules in Excel VBA. It explains that subs and functions need to be public to be callable, and how to use Call or Application.Run to invoke code in the same or different workbooks. It also provides examples of passing arguments and checking if a workbook is open before trying to call its code.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views5 pages

Calling Macro From Workbook

This document discusses different ways to call subs and functions between worksheets and modules in Excel VBA. It explains that subs and functions need to be public to be callable, and how to use Call or Application.Run to invoke code in the same or different workbooks. It also provides examples of passing arguments and checking if a workbook is open before trying to call its code.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ask Question

3
I am trying to call a Sub I defined inside a sheet from a module.

The code I use in my module (Inside a Sub)

 Try Call Worksheets("Sheet_name").Sub_name – Scott Craner Nov 20 '15 at 16:40


 Do you get an error? If so, what is it? – Rory Nov 20 '15 at 16:44
 works like a charm, tanks you! – user3614882 Nov 20 '15 at 16:46
add a comment
4 Answers
activeoldest votes

7
You need to set your function to Public as shown below
Sheet1:

Public Sub test()


MsgBox "Got here!"
End Sub
Module:

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 Worksheets("Sheet 20").CommandButton1_Click

How do I use Application.Run in Excel


When you want to run a macro from an event or from another macro in the same
workbook you can call the macro like this in your code :

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"

If the workbook name includes spaces or some other particular characters it is


necessary to enclose the name with single quotes, like this :

Application.Run "'Book 1.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"

Callbacks instead of macros in Excel 2007 -2016

But what if you use Excel 2007-2016 and use custom Ribbon controls with
callbacks.

A normal macro looks like this :

Sub TestMe()
MsgBox "Hi there"
End Sub

And a callback looks like this :

Sub TestMe(control As IRibbonControl)


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.

But we can do this to call a callback in the same workbook :

Sub test1()
Dim obj As Object
TestMe obj
End Sub

Or to call a callback in an add-in or another workbook use :

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

Check if file or add-in is open


Before you try to run the Application.Run line that call a macro or callback in
another workbook or add-in you can test if the workbook or add-in is open with the
code below.

Sub ErrorTest()
Dim TestWkbk As Workbook
Dim obj As Object

Set TestWkbk = Nothing


On Error Resume Next
Set TestWkbk = Workbooks("RDBTestAdd-in.xlam")
On Error GoTo 0

If TestWkbk Is Nothing Then


MsgBox "Sorry the File is not open, it is not possible to run the
macro." & _
" But you can add code here to open the workbook or add-in."
Else
MsgBox "Use one of the two lines below to call the callback or macro"

'Run a callback in a Excel 2007-2016 workbook/add-in


'Application.Run "'" & TestWkbk.Name & "'!RunMyMacro", obj

'If you want to run a macro in a Excel 97-2016 workbook/add-in use


'Application.Run "'" & TestWkbk.Name & "'!RunMyMacro"

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.

Returning values from functions or passing arguments to


functions or macros

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.

Function IsOpen(WBname As String) As Boolean


Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(WBname)
If Err = 0 Then IsOpen = True
End Function

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.

Dim MyResult As Boolean


MyResult = Application.Run("'" & TestWkbk.Name & "'!IsOpen", "RDBMerge.xlam")

Or use this to call a macro/function with arguments


Application.Run "'" & TestWkbk.Name & "'!MacroNameHere", "parm1", "parm2"

Acknowledgements

Many thanks to Peter Thornton, Jim Rech, Dave Peterson and Mike Rosenblum for
their useful comments.

You might also like