VBA Sub Procedure

Sub Procedures

Sub Procedures are similar to functions but there are few differences.
  • Sub procedures DONOT Return a value while functions may or may not return a value.
  • Sub procedures Can be called without call keyword.
  • Sub procedures are always enclosed within Sub and End Substatements.

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
1. Now we will be able to call the function only but not the sub procedure as shown below.
sub_procedure in VBA
2. The Area is calculated and shown only in Message box.
calculate_area_sub_2 in VBA
3. 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 subprocedure from the excel worksheet.
calculate_area_sub_3 in VBA