O'Reilly logo

Excel VBA Programming For Dummies, 5th Edition by John Walkenbach, Michael Alexander

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 (two ways)

In the preceding chapters, you’ve seen the term Sub procedures and Function procedures. The differences between the two kinds of procedures are probably still a mystery to you at this point, but fear not. This chapter clears up any confusion you may have about these concepts.

Understanding 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 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, an array).

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 depends on the Sub procedure’s VBA code.)

A Function is also a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required