Chapter 5

VBA Sub and Function Procedures

IN THIS CHAPTER

Bullet Understanding the difference between Sub procedures and Function procedures

Bullet Executing Sub procedures (many ways)

Bullet Executing Function procedures (three ways)

In previous chapters, I use the terms Sub procedures and Function procedures without going into much explanation about what these procedures do or how to use them. This chapter fills in those gaps. It explains the differences between Sub and Function procedures, and shows the various ways to execute each type of procedure.

Understanding Subs versus Functions

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

  • A Sub procedure is a group of VBA statements that performs an action (or a sequence of actions) with Excel.
  • A Function procedure is a group of VBA statements that performs a calculation and returns a single value (or, sometimes, something slightly more complicated like a single object).

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

Get Excel VBA Programming For Dummies, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.