Chapter 3

advertisement
Chapter 3
The Visual Basic Editor
Important Features of the VBE
•
•
•
•
Alt-F11 will open the Visual Basic Editor.
The Code window is to the right, Project Explorer window and the Properties window.
Project Explorer displays a list of all open projects. Doubling-clicking on a particular
sheet will open up the appropriate code window .
Below a given project, the Project Explorer window shows its “elements”.
–
•
The Properties window lists a set of properties for the selected object.
–
•
•
•
•
For example, the Sheet object has twelve properties listed.
The VBE has three very useful toolbars: the Standard, Edit, and Debug toolbars.
Standard tool bar-run, pause, or stop a program you have written.
–
•
These include an element called ThisWorkbook, and any modules (VBA code), user forms (for dialog
boxes), or references associated with the project.
Also, allows you to display the Project or Properties windows if hidden. You can also display the Object
Browser and the Control Toolbox.
Editing toolbar allows indenting or outdenting, comment or uncomment blocks of code
in addition to the usual editing features of copy, paste, clear, cut, find, replace to
mention a few.
Debug toolbar allows you to track down errors in you program code that are not
apparent.
Other useful menu items include Insert/Module and Insert/UserForm.
The Object Browser
• Contains a list of object model libraries.
• The Excel and VBA libraries will be of the most
importance.
• The Excel library provides help on all of the objects ,
their properties and methods in the Excel object model.
• The VBA library provides help on the VBA elements that
are common to all applications that can use VBA: Excel,
Access, Word, Powerpoint to name a few.
• A property is denoted by a hand icon and a method is
denoted by a green rectangular icon.
• You can receive help by clicking on the question mark
icon.
Immediate and Watch Windows
• Immediate window can enter one-line VBA commands.
– Press Enter and the command takes effect immediately.
– Precede the command by a question mark and you will get an
immediate answer to a question.
– Debug.Print followed by something to be printed to the Immediate
window. You can use this to check the output at various points in
the program.
• Watch window is used for debugging. Programs have
several variables that change values over time.
• If the program is not running properly, you can put a watch
on one or more key variables to see how they change as
the program progresses
A First Program
•
•
•
•
•
•
•
•
Open the file
Get into the VBE (Alt+F11)
Add a module
Start a Sub
Type the code
Run the program from the VBE
Run the program with a button
Save the file
Download