![VBA Tutorial](/vba/images/vba-mini-logo.jpg)
- VBA Tutorial
- VBA - Home
- VBA - Overview
- VBA - Excel Macros
- VBA - Excel Terms
- VBA - Macro Comments
- VBA - Message Box
- VBA - Input Box
- VBA - Variables
- VBA - Constants
- VBA - Operators
- VBA - Decisions
- VBA - Loops
- VBA - Strings
- VBA - Date and Time
- VBA - Arrays
- VBA - Functions
- VBA - Sub Procedure
- VBA - Events
- VBA - Error Handling
- VBA - Excel Objects
- VBA - Text Files
- VBA - Programming Charts
- VBA - Userforms
- VBA Useful Resources
- VBA - Quick Guide
- VBA - Useful Resources
- VBA - Discussion
VBA - Sub Procedure
Sub Procedures are similar to functions, however there are a few differences.
Sub procedures DO NOT Return a value while functions may or may not return a value.
Sub procedures CAN be called without a call keyword.
Sub procedures are always enclosed within Sub and End Sub statements.
Example
Sub Area(x As Double, y As Double) MsgBox x * y End Sub
Calling Procedures
To invoke a Procedure somewhere in the script, you can make a call from a function. We will not be able to use the same way as that of a function as sub procedure WILL NOT return a value.
Function findArea(Length As Double, Width As Variant) area Length, Width ' To Calculate Area 'area' sub proc is called End Function
Now you will be able to call the function only but not the sub procedure as shown in the following screenshot.
![Sub Procedure in VBA](/vba/images/sub_procedure.jpg)
The area is calculated and shown only in the Message box.
![Calculate Area Sub 2 in VBA](/vba/images/calculate_area_sub_2.jpg)
The result cell displays ZERO as the area value is NOT returned from the function. In short, you cannot make a direct call to a sub procedure from the excel worksheet.
![Calculate Area Sub 3 in VBA](/vba/images/calculate_area_sub_3.jpg)