Four Ways to Run Your Program
Running your program from within the Visual Basic Editor is fine when you want
to test it. However, the goal is to run it from the application and not have to
open the Visual Basic Editor first. You have a lot of choices for running any
VBA program — more than most people want to remember.
VBA provides
four common methods for running applications, but most VBA users never
need to think beyond the first method, which is using the Macro dialog box.
Using the Macro dialog box
The Macro dialog box is the most common way to run a VBA program. Every
time
that you create a new
Sub
, it appears in the list of macros that you can
run. You don’t have to do anything special. This feature means that you can
always access every program that you create by using this method, which is
why it’s the most popular method.
When working
with older versions of Office, use the Tools
➪
Macro
➪
Macros
command to display the Macro dialog box. When using newer versions of
Office, click Macros on the Developer tab on the Ribbon to display the Macro
dialog box, as shown in Figure 2-5. You can also display this dialog box by
pressing Alt+F8 in Office applications. (Other applications
use different key
combinations.) Notice that Figure 2-5 shows the macro,
SayHello
, that
appears in the earlier “Step 2: Implement the design” section of this chapter.
When you want to run the program, highlight it and then click the Run button.
Try it now. You should see the same two dialog box sequences that you saw
when you tested the program earlier in this chapter.
40
Part I: An Overview of VBA
The origins of computer bugs
You might wonder why professionals call a pro-
gramming error a
bug. There are many versions
of this story, but all
of them begin in the early
annals of computer history. At that time, com-
puters were immense devices that could take up
an entire building. Instead of the small chips used
in today’s computers,
these older devices used
switches and vacuum tubes. The story holds
that one day a programmer of one of these mas-
sive machines experienced an error. After a
long search, the programmer found an actual
bug in one of the
switches used to control the
computer. The term stuck — all programming
errors are now blamed on insects.
06_046500 ch02.qxp 12/5/06 5:33 PM Page 40
You can also use this dialog box to perform other tasks. Highlight the macro
name, and then click the Edit button; the application opens the Visual Basic
Editor. The Code window displays the code associated
with the program that
you highlighted. You can also remove old programs by clicking the Delete
button.
Notice the Macro Name field near the top of the Macro dialog box shown in
Figure 2-5. This field normally contains the name of the macro that you’ve
highlighted in the list. However, you can also use the Macro dialog box to
create new programs. Type
Do'stlaringiz bilan baham: