Writing Your Own Macros
Letting VBA do all the work by recording your macros is an easy way to automate tasks, and it's a technique you'll use often. However, to get the most out of VBA you need to do some full-fledged programming, which means writing your own macros, either from scratch or by using a recorded macro as a starting point.
Why go to all that trouble? Here are just a few of the advantages you gain by doing this:
- If you make a mistake while recording a macro, particularly one that requires a large number of steps, you can make a simple edit to the macro's VBA code to fix the mistake rather than re-record the whole thing from scratch.
- You get full control over each macro, which means you ensure that your macros do exactly what you need them to do.
- You can take advantage of the hidden power of VBA to manipulate the Office programs and to perform some impressive programming feats that are simply not available via the recording process.
To help you realize these advantages and many more, this tutorial introduces you to the basics of writing simple procedures and functions, as well as how to get around in the Visual Basic Editor, which is the tool that VBA provides for writing macros by hand.
In this tutorial:
- Activating the Ribbon's Developer Tab
- Displaying the Visual Basic Editor
- Touring the Visual Basic Editor
- Understanding VBA Procedures
- Creating a Command Macro
- Entering VBA Statements
- Creating a User-Defined Function
- Understanding User-Defined Functions
- Writing a User-Defined Function
- Using a Function
- Advantage of VBA IntelliSense
- Shutting Down the Visual Basic Editor