Collapse the table of content
Expand the table of content
Expand Minimize

Writing a Sub Procedure

Office 2010
This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010.

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements that performs actions but doesn't return a value. A Sub procedure can take arguments, such as constants, variables, or expressions that are passed by a calling procedure. If a Sub procedure has no arguments, the Sub statement must include an empty set of parentheses.

The following Sub procedure has comments explaining each line.

' Declares a procedure named GetInfo 
' This Sub procedure takes no arguments 
Sub GetInfo() 
' Declares a string variable named answer 
Dim answer As String 
' Assigns the return value of the InputBox function to answer 
answer = InputBox(Prompt:="What is your name?") 
 ' Conditional If...Then...Else statement 
 If answer = Empty Then 
 ' Calls the MsgBox function 
 MsgBox Prompt:="You did not enter a name." 
 ' MsgBox function concatenated with the variable answer 
 MsgBox Prompt:="Your name is " & answer 
 ' Ends the If...Then...Else statement 
 End If 
' Ends the Sub procedure 
End Sub 

© 2015 Microsoft