Table of contents
TOC
Collapse the table of content
Expand the table of content

Writing a Sub Procedure

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

A Sub procedure is a series of Visual Basicstatements enclosed by the Sub and End Sub statements that performs actions but doesn't return a value. A Sub procedure can take arguments, such asconstants, 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." 
 Else 
 ' 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
© 2017 Microsoft