Microsoft Office Tutorials and References
In Depth Information
Understanding Functions Versus Subs
Understanding Functions Versus Subs
Excel VBA macros come in two distinct varieties: Sub procedures and Function procedures. This
tip describes how they differ.
VBA Sub procedures
You can think of a Sub procedure as a new command. You can have any number of Sub
procedures in an Excel workbook. Figure 206-1 shows a simple VBA Sub procedure. When this code is
executed, it toggles the page break display for the active worksheet.
Figure 206-1: A simple, but useful, VBA procedure.
Sub procedures always start with the keyword Sub, the macro’s name, and then a pair of
parentheses. The End Sub statement signals the end of the procedure. The lines between this keyword
and statement comprise the procedure’s code.
You execute a VBA Sub procedure in several ways. For example, you can press Alt+F8 and then
select the macro from the Macros dialog box. For other ways to execute Sub procedures, see
Tip 205.
When you record a macro, it’s always a Sub procedure.
VBA functions
A second type of VBA procedure is a Function procedure, which always returns a single value
(just as a worksheet function always returns a single value). A VBA Function procedure can be
executed by other VBA procedures or used in worksheet formulas, just as you would use the
built-in Excel worksheet functions.
 
Search JabSto ::




Custom Search