편집

다음을 통해 공유


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.

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

Note

Interested in developing solutions that extend the Office experience across multiple platforms? Check out the new Office Add-ins model. Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

Call 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 it 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

Use 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 are 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 don't assign the function to a variable, as shown in the following example.

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

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

Pass named arguments

A statement in a Sub or Function procedure can pass values to called procedures by 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 by using named arguments with no return value.

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

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

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

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.