1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise
1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise
0 VB Editor
FLASH EXERCISE:
You should have the VB Editor open prior to going to the next exercise.
1.2 Parts of the VB Editor
The VB Editor has four main parts:
You can adjust the relative size of the panes by moving your cursor over one of the borders,
clicking and dragging.
You can also close any pane. The View menu command will let you get them back.
The different panes / windows can also be re-arranged by dragging them by the title bar.
Tool Bar/Menu Bar
Properties Window
The Project Explorer shows a tree view of all the objects in the current instance of Excel. In our
case, it shows the new workbook we have open and the objects associated with it.
Note that, in most cases, there are two ways each object is identified. For example, the first
sheet has both an object name of "Sheet1" and a sheet name of Sheet1. The name in parenthesis
is the name of the sheet as you see it on a sheet tab in the workbook. The first "Sheet 1", the
name that is not in parenthesis, is the object name of the worksheet. You can use this object
name in VBA code to refer directly to the worksheet. We will work more with this later.
The majority of our work is done in code windows, or modules. There are two main ways to
open up modules in the VB Editor.
FLASH EXERCISE:
Add image
Before continuing to the next page, you should have a code window open in the VB Editor.
Project properties can be modified during design time (when working on the project) using the
Properties window. Properties can be viewed by category or in alphabetic order.
The Properties window is often used for naming modules, sheets, or other objects. We will use it
most when working in the VB Editor with UserForms.
A VBA object (for example, a "Worksheet" object) has "properties" and "methods".
Properties are the characteristics of the object. Methods are the actions associated with the
object. This is treated in more detail in Chapter 5, Properties and Methods lesson. You can click
on the preceding link to learn more about properties and methods, then use the back button on
your browser to return to this lesson page.
Worksheets have two names: a code object Name, and the more visible sheet Name.
You can view and change these names in the VB Editor.
How to refer to worksheets using either the object name or the sheet name.
One way to run VBA procedures.
FLASH EXERCISE:
In the preceding Flash Exercise, you wrote two VBA procedures. Each procedure was bracketed by a Sub
and an End Sub statement. Code structure and basic code blocks are explained in the Chapter 2, Code
Blocks. You can click on the preceding link to see more explanation about code blocks. Just use the back
button on your browser to return to this lesson.
FLASH EXERCISE:
Run, Break, Reset: this set of buttons starts, pauses, or resets the sub or User
Form.
Toggle Breakpoint: Sets a breakpoint (a place where the code will stop, and
puts VB Editor into break mode) at a code line. Same thing can be
accomplished using F9.
Step Into: steps through the procedure one line at a time. You will usually
use the F8 key instead, since it is easier.
The workbook can be saved from the VB Editor by using Ctrl + S, menu command File | Save,
or by using the Save button.
Note: Menu command File | Save As is not available from the VB Editor. If you want to save
your project with a new name, you need to do it from the Excel workbook.
Stepping Through Code in Break Mode
When you are writing VBA and building Excel applications, you may spend a lot of time
stepping through code. What does that mean?
Normally, code is triggered by some event (like a button being clicked), then the processing
dictated by the code runs and completes. The user never sees the code, only the results of it
running.
In break mode, the code can be “stepped through” in various ways. This allows you, as the
programmer, to control the step-by-step running of the code. You can cause the processing to
execute a single code line at at time or cause processing to stop at key locations in the
program. This allows you to scrutinize code performance as the code is running. It is like
watching a film frame by frame or in slow motion, with the capability of stopping the film at
any frame. This is an important feature of the VB Editor and a great help for debugging
programs.
FLASH EXERCISE:
1. Go back to the VB Editor to the session where you wrote the sheet activation code; it
should look something like the picture below.
2. Start the Flash Exercise below and follow the directions. You will step through the
code you wrote in the earlier lesson.
The following Flash Exercise shows how recording and modifying a macro can be used to solve
the business scenario.
Chapter 1 Summary
The following subjects were presented in this chapter:
The next chapter introduces the main code blocks used in VBA. You will use these blocks in
several exercises. Some of these exercises consist of writing useful code snippets that you can
use over and over again.