VBA Sub procedures

Subprocedure can be treated as a new command like other commands in Excel. Any user of your workbook can use this command. A Sub procedure can also be executed by other Macro. You can have any number of Sub procedures in an Excel workbook.

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

Executing a Subprocedure


A VBA Sub procedure can be executed using any of the following ways:
  • Choose Developer Code Macros or press Alt+F8 to display the Macro dialog box. Select the procedure name from the list, and then click Run to execute the Subprocedure


Macro dialog box. Select a Sub procedure from the list.

  •  Press the procedure’s shortcut key combination (if it has one).
  • Click a button or other shape that has a macro assigned to it.
  • If the VB Editor is active, move the cursor anywhere within the code and press F5.
  • Execute the procedure by calling it from another VBA procedure.



Post a Comment

0 Comments