MS-Excel / General Formatting

Understanding VBA Procedures

Before you get to the nitty-gritty of writing your own macros, let's take a second to understand what exactly you'll be writing. When you create VBA code by hand what you're creating is something called a procedure. In VBA, a procedure is, broadly speaking, a collection of related statements that forms a unit and performs some kind of task. (A statement is an instruction that tells VBA to perform a specific task.) VBA procedures come in two flavors: command macros and user-defined functions. Here's a summary of the differences:

  • Command macros are the most common types of procedures; they usually contain statements that are the equivalent of Ribbon options and other program commands. The distinguishing feature of command macros is that, like regular application commands, they have an effect on their surroundings. (In Word, for example, this means the macro affects the current document, a section of text, and so on.) Whether it's opening a document, formatting some text, or inserting a paragraph, command macros change things.
  • User-defined functions work just like a program's built-in functions. Their distinguishing characteristic is that they accept input values and then manipulate those values and return a result.
[Previous] [Contents] [Next]