Chapter 5
VBA Sub and Function Procedures
IN THIS CHAPTER
Recognizing the difference between Sub procedures and Function procedures
Executing Sub procedures (many ways)
Executing Function procedures (three ways)
In previous chapters, I use the terms Sub procedures and Function procedures without 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 perform an action (or a sequence of actions) with Excel.
- A Function procedure is a group of VBA statements that perform a calculation and return 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 what happens ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access