0% found this document useful (0 votes)
84 views

1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise

1. The document discusses opening the VB Editor in Excel and describes its main parts: the toolbar/menu area, project explorer pane, properties pane, and code window. 2. It explains how to insert a code module in the VB Editor and discusses the project explorer window and properties window. 3. Steps are provided for activating worksheets using code, adding toolbars to the VB Editor, and stepping through code in break mode. Recording a macro and modifying code is also summarized.

Uploaded by

tejsri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise

1. The document discusses opening the VB Editor in Excel and describes its main parts: the toolbar/menu area, project explorer pane, properties pane, and code window. 2. It explains how to insert a code module in the VB Editor and discusses the project explorer window and properties window. 3. Steps are provided for activating worksheets using code, adding toolbars to the VB Editor, and stepping through code in break mode. Recording a macro and modifying code is also summarized.

Uploaded by

tejsri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

0 VB Editor

1.1 Opening the VB Editor


The Visual Basic Editor (VB Editor or VBE) is part of the Excel application. It is only available
when Excel is opened. If Excel is closed, so is the editor.

FLASH EXERCISE:

1. Open an Excel workbook


2. Click on the link below and learn three ways to open the VB Editor. Follow the directions
and customize your Excel toolbar by adding a VB Editor toolbar button. When you are
finished with the exercise, you should have the VB Editor open.

            Open the VB Editor

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:

 Toolbar / Menu area


 Project explorer pane
 Properties pane
 Code window (not shown yet)

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

Project Explorer Window

Properties Window

1.2.1 Project Explorer 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.

1.2.2 Inserting a Code Module

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:

1. Open the VB Editor


2. Click on the following link to start the Flash video. Follow the directions and use two
different methods to insert a module into the code pane of the VB Editor.

Add image

Before continuing to the next page, you should have a code window open in the VB Editor.

1.2.3 Properties Window

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.

What are "Properties"?

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.

Referring to Worksheets with Code


In the following Flash Exercise, you will learn:

 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:

1. Open your VB Editor with a blank code module.


2. Follow the directions in the following Flash video, and write two code blocks that
activate a worksheet.

Activate worksheets with code

What is this "Sub...End Sub"?

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.

Toolbars in the VB Editor


When the VB Editor is first used, only the Standard toolbar is active. Some additional toolbars
are available that help with writing, formatting, and debugging code. In the following exercise,
you will activate these toolbars.  Later in the course, you will use them.

FLASH EXERCISE:

1. Open the VB Editor


2. Click on the link below to run a Flash video. Follow the directions and add two useful
toolbars to the VB Editor.

              Adding VB Editor toolbars


Some of the buttons you will use the most in this course are introduced below.

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.

Indent and Outdent a single line or block of code

Comment or Uncomment a single line or block of code. Commenting puts


an apostrophe in front of the line, and the VB Editor ignores it.

Saving Your Work


The VBA code is part of the workbook.  Whenever the workbook is saved, the code is saved.

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.

        Stepping through code


Record a Macro and Modify the Code
One of the quickest ways to discover Excel VBA syntax is to record a macro. Recorded macros
can be somewhat limited and will always use literal values for objects such as cells and
worksheet names. This is often undesirable. Fortunately, recorded macros can be modified to
make them more dynamic or to extend their functionality. The syntax of some Excel tasks is
particularly well suited to being discovered using recorded macros:

 Copying and pasting ranges


 Inserting and/or deleting worksheets
 Opening a new workbook or moving between workbooks
 Moving between worksheets
Recorded macros will not contain certain VBA coding structures such as loops, decision-making
If or Select Case blocks, or With blocks. These VBA blocks must be manually coded.

Following is a possible business scenario:

1. A report is generated in a workbook.


2. Each time the report is run, the refreshed data is saved off to a separate workbook.
3. It is desirable to partially automate this repetitive procedure.

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 VB Editor (VBE): how to open it and its main components


 How to insert a code module
 How to use the Properties pane to modify properties of an object; our example is the
worksheet object
 The two main ways to refer to a worksheet in VBA; in our example we activated a
worksheet using code
 How to step through code
 Recording a macro and modifying the code

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.

You might also like