Now that you've learned a bit about the Visual Basic editor and how Excel uses objects, it's time to jump right in and get some firsthand experience with VBA. First, you'll see how custom-programmed macros can perform some basic editing and formatting. After that, you'll learn how you can use simple macro code to solve problems that you can't tackle with the macro recorder alone. In these sections, you'll see how to manipulate data more flexibly, make decisions, and repeat actions.
Before you get started, here are a few helpful tips:
For easier macro coding, try to arrange your windows so you can see the Excel window and the Visual Basic editor window at the same time, side-by-side.
To play a macro, move to the position in the worksheet where the macro should execute, and then switch to the Visual Basic editor. In the Visual Basic editor, move to the appropriate subroutine and click the play button.
Make sure you type in every command and object name correctly. Minor differences cause mysterious errors.
If Excel finds some invalid code when running a macro, it stops executing it and highlights the problem line in yellow. At this point, you can correct the line and press play to keep going, or stop and give up altogether (many programmers find long walks on the beach or guzzling a jug of Mountain Dew a helpful way to clear their heads).
A few examples can go a long way to showing you how a typical macro works. First of all, check ...