Ad space (banner)
Lesson 7 / 20

Writing a Sub procedure

This lesson covers the Sub procedure, the basic way VBA gathers work together.

A Sub procedure is a unit of work that returns no value, defined as Sub Name(args) ... End Sub. Other procedures call it with Call Name(args), or simply Name args.

The sample code defines Sub Greet(name As String) ... End Sub and calls it with Call Greet("Bob").

A common early stumble is the difference from the Function procedure covered in the next lesson. "A Sub when it just does work and returns nothing, a Function when you want a calculated result back" makes the choice easy.

In professional work, gathering logic into a Sub procedure in one place, rather than copying it around, is what keeps a macro maintainable.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)