Types of Procedures

Visual Basic allows you to create two types of procedures: Sub procedures and Function procedures.

A Sub procedure is a unit of code enclosed between the Sub and End Sub statements that performs a task but doesn't return a value.

A Function procedure is a unit of code enclosed between the Function and End Function statements. Like a Sub procedure, a Function procedure performs a specific task. However, a Function procedure also returns a value you can use in an expression, as shown in the following example.


totalSales = CalculateTotal(x)    'CalculateTotal is a Function procedure

You can also use Function procedures in place of complex worksheet formulas, just as you use built-in Visual Basic functions such as Sqr, Cos, and Chr.

Note

A Function procedure you call from a formula on a worksheet shouldn't make changes to the data in a workbook or change the Microsoft environment in any way. For more information, see "Using Function Procedures on Worksheets" later in this chapter.