Subs Versus Functions

The VBA code that you write in the Visual Basic Editor is known as a procedure. The two most common types of procedures are Sub and Function.

1 A Sub procedure is a group of VBA statements that performs an action (or actions) with Excel.

1 A Function procedure is a group of VBA statements that performs a calculation and returns a single value.

Most of the macros you write in VBA are Sub procedures. You can think of a Sub as being like a command: Execute the Sub procedure and something happens. (Of course, exactly what happens depends on the Sub procedure's VBA code.)

A Function is also a procedure, but it's quite different from a Sub. You're already familiar with the concept of a function. Excel includes many worksheet functions that you use every day (well, at least every weekday). Examples include SUM, PMT, and VLOOKUP. You use these worksheet functions in formulas. Each function takes one or more arguments (although a few functions don't use any arguments). The function does some behind-the-scenes calculations and returns a single value. The same goes for Function procedures that you develop with VBA.

Was this article helpful?

0 0

Post a comment