Executing a macro, inside a macro

Lynxador

Board Regular
Joined
Jan 6, 2005
Messages
125
is there anyway to execute a macro from inside a macro? i've looked everywhere befor i came here just can't find the answer.

Im basically building a macro, that will use 4 different macros inside my personal module folders. but im not quite sure how to do it.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

XLML

Active Member
Joined
Aug 15, 2003
Messages
407
Hi Lynxador,

Try using VBE's Call feature. For instance, if you want your current macro (Summary) to excecute another (Detail), it would be:

Sub Summary()
Call Detail
End Sub


XLML
 

Nimrod

MrExcel MVP
Joined
Apr 29, 2002
Messages
6,259
HERE'S A DEMO FOR YOU ... :)

Public Sub MainMacro()
Call Demo1
Call Demo2("hello")
MsgBox "Demo3 returns value " & Demo3(5)
End Sub

Private Sub Demo1()
MsgBox "Demo 1"
End Sub

Private Sub Demo2(Str As String)
MsgBox "Demo 2 " & Str
End Sub

HERE'S WHAT YOUR VBA HELP SAY'S ON THE SUBJECT :wink: :wink:

Calling Sub and Function Procedures


To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses.

You can use a Sub procedure to organize other procedures so they are easier to understand and debug. In the following example, the Sub procedure Main calls the Sub procedure MultiBeep, passing the value 56 for its argument. After MultiBeep runs, control returns to Main, and Main calls the Sub procedure Message. Message displays a message box; when the user clicks OK, control returns to Main, and Main finishes.

Sub Main()
MultiBeep 56
Message
End Sub

Sub MultiBeep(numbeeps)
For counter = 1 To numbeeps
Beep
Next counter
End Sub

Sub Message()
MsgBox "Time to take a break!"
End Sub

Calling Sub Procedures with More than One Argument
The following example shows two ways to call a Sub procedure with more than one argument. The second time HouseCalc is called, parentheses are required around the arguments because the Call statement is used.

Sub Main()
HouseCalc 99800, 43100
Call HouseCalc(380950, 49500)
End Sub

Sub HouseCalc(price As Single, wage As Single)
If 2.5 * wage <= 0.8 * price Then
MsgBox "You cannot afford this house."
Else
MsgBox "This house is affordable."
End If
End Sub

Using Parentheses when Calling Function Procedures
To use the return value of a function, assign the function to a variable and enclose the arguments in parentheses, as shown in the following example.

Answer3 = MsgBox("Are you happy with your salary?", 4, "Question 3")

If you're not interested in the return value of a function, you can call a function the same way you call a Sub procedure. Omit the parentheses, list the arguments, and do not assign the function to a variable, as shown in the following example.

MsgBox "Task Completed!", 0, "Task Box"

Caution If you include parentheses in the preceding example, the statement causes a syntax error.

Passing Named Arguments
A statement in a Sub or Function procedure can pass values to called procedures using named arguments. You can list named arguments in any order. A named argument consists of the name of the argument followed by a colon and an equal sign :)=), and the value assigned to the argument.

The following example calls the MsgBox function using named arguments with no return value.

MsgBox Title:="Task Box", Prompt:="Task Completed!"

The following example calls the MsgBox function using named arguments. The return value is assigned to the variable answer3.

answer3 = MsgBox(Title:="Question 3", _
Prompt:="Are you happy with your salary?", Buttons:=4


Public Function Demo3(Num) As Integer
Demo3 = Num * 2
End Function
 

Forum statistics

Threads
1,148,396
Messages
5,746,452
Members
424,020
Latest member
LongDoo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top