+ All Categories
Home > Documents > Using the Visual Basic Editor Visual Basic for Applications 1.

Using the Visual Basic Editor Visual Basic for Applications 1.

Date post: 21-Dec-2015
Category:
View: 241 times
Download: 8 times
Share this document with a friend
Popular Tags:
69
Using the Visual Basic Editor Visual Basic for Applications 1
Transcript
Page 1: Using the Visual Basic Editor Visual Basic for Applications 1.

Using the Visual Basic Editor

Visual Basic for Applications

1

Page 2: Using the Visual Basic Editor Visual Basic for Applications 1.

Objectives

In this tutorial, you will learn how to: Recognize the components of the Visual Basic Editor

Enter code using the Visual Basic Editor

Create a procedure

Internally document a procedure

Manipulate the Office Assistant using code

Display a message in a dialog box

Run, save, and print a procedure

1

Page 3: Using the Visual Basic Editor Visual Basic for Applications 1.

Concept Lesson:Procedures

Visual Basic for Applications (VBA) is the programming language found in many Microsoft and non-Microsoft products

You can use VBA to customize an application to fit your needs

You do so by creating a procedure, which is simply a series of VBA instructions grouped together as a single unit for the purpose of performing a specific task

1

Page 4: Using the Visual Basic Editor Visual Basic for Applications 1.

Procedures

You can run some procedures, called macros, directly from the Macros dialog box

Other procedures, called event procedures, run in response to specific actions you perform on an object

Those actions—such as opening a document, activating a worksheet, or clicking a command button—are called events

1

Page 5: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

The host application is the application in which you are working

The Visual Basic Editor contains three separate windows:– The main window

– The Project Explorer window

– The Properties window

Visual Basic Editor will have a similar interface in every application in which it is contained

The main window, at the top of the screen, contains the title bar, the menu bar, and the Standard toolbar

1

Page 6: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor Opened in Microsoft Excel

1

Page 7: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

The Project Explorer window displays a list of the open projects and their components

A Module object is simply a container that stores macros and other procedures that are not associated with any specific object in the project

Every VBA object has a set of characteristics, called properties, associated with it that control the object’s appearance and behavior

These properties, along with their values, are listed in the Properties window

1

Page 8: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

The Toggle Folders button controls the display of the folders in the Project Explorer window

The Project Explorer window operates similarly to the Windows Explorer window in that a plus sign next to a folder indicates that the folder contains objects

You can use the View Object button to view the object whose name is selected in the Project Explorer window

When an object is selected in the Project Explorer window, you can use the View Code button to open its Code window

1

Page 9: Using the Visual Basic Editor Visual Basic for Applications 1.

Project Explorer Window with the Toggle Folders Button Toggled On and Off

1

Page 10: Using the Visual Basic Editor Visual Basic for Applications 1.

Project Explorer Window Showing Minus and Plus Signs

1

Page 11: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

In the Code window, you enter the VBA instructions, called code, that instruct a procedure on how to perform a task

You enter your VBA instructions between the Public Sub and End Sub lines in the Code window

The Public Sub line denotes the beginning of the procedure whose name follows the word Sub, and the End Sub line marks the end of the procedure

A keyword is a word that has a special meaning in a programming language

1

Page 12: Using the Visual Basic Editor Visual Basic for Applications 1.

Code Window Showing the MorningMsg Procedure

1

Page 13: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

A sub procedure refers to a block of code that performs a specific task, but does not return a value

Function procedures, which are designated by the keyword Function, are procedures that can returna value

The keywords Public and Private indicate the procedure’s scope, which determines which objects can use the procedure

A Public scope indicates that the procedure can be used by all objects within the project

1

Page 14: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

A Private scope indicates that the procedure can be used only by the object in which the procedure is contained

The keyword End indicates the end of something The `display message line is a comment The Object list box either will display the word

(General), or it will display the type of object associated with the Code window

The Procedure list box displays the name of the current procedure

1

Page 15: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor1

Page 16: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor

Sometimes scroll bars will appear on the Procedure list box to indicate that not all of the procedures are currently displayed

In VBA the Assistant object represents the Office Assistant, and its FileName property controls the character used to display the Office Assistant

When the Procedure View button is selected, each procedure associated with the selected object appears in its own Code window

When the Full Module View button is selected, all of the current object’s code appears as a single listing in the Code window

1

Page 17: Using the Visual Basic Editor Visual Basic for Applications 1.

Maximized Code Window with the Full Module View

Button Selected

1

Page 18: Using the Visual Basic Editor Visual Basic for Applications 1.

Entering Instructions in the Code Window

You can enter VBA instructions into a Code window by directly typing each instruction in its entirety, or the Visual Basic Editor can assist you in entering the instructions

The Visual Basic Editor can provide assistance in two ways:– by displaying a listing of an object’s properties and

methods after you type the object’s name followed by a period in the Code window

– by displaying the syntax, or programming language rules, of a command as you are entering it in the Code window

1

Page 19: Using the Visual Basic Editor Visual Basic for Applications 1.

Entering Instructions in the Code Window

A method is a predefined VBA procedure, which is simply a procedure that the Microsoft programmers have already coded for you

If your preference is to have the Visual Basic Editor assist you when entering instructions, you need to select both the Auto List Members and Auto Quick Info check boxes, which are located on the Editor tab in the VB Editor’s Options dialog box

When the Auto List Members check box is selected, the Visual Basic Editor will display an object’s members after you type the object’s name followed by a period

1

Page 20: Using the Visual Basic Editor Visual Basic for Applications 1.

Entering Instructions in the Code Window

1

Page 21: Using the Visual Basic Editor Visual Basic for Applications 1.

Entering Instructions in the Code Window

The properties and methods are listed in alphabetical order, and the list contains a scroll bar, which indicates that not all of the members can be viewed at the same time

The period between the object and its property is known as the dot member selection operator and it indicates that the FileName property is a member of the Assistant object

When the Auto Quick Info check box is selected on the Editor tab of the Options dialog box, the Visual Basic Editor displays the syntax of the command you are typing in the Code window

1

Page 22: Using the Visual Basic Editor Visual Basic for Applications 1.

Entering Instructions in the Code Window

The term syntax refers to the rules of a programming language

According to the syntax, you can include five items of information, called arguments, in the MsgBox command

1

Page 23: Using the Visual Basic Editor Visual Basic for Applications 1.

Saving a Procedure

You save a VBA procedure by saving the file that contains the procedure

You can use the Save command on the File menu, the Save button on the Standard toolbar, or the key combination Ctrl+S to save the file

After saving a procedure, you then run it to verify that it is working correctly

1

Page 24: Using the Visual Basic Editor Visual Basic for Applications 1.

Running a Procedure

While in the Visual Basic Editor, you can use either the Run menu or the Tools menu to run a procedure

You also click the Run procedureType button on the Standard toolbar or you can press the F5 key

The method you use to run a procedure from the host application depends on the type of procedure being run

Event procedures run automatically in response to the occurrence of an event, such as the opening of a document or the clicking of a command button

1

Page 25: Using the Visual Basic Editor Visual Basic for Applications 1.

Printing a Procedure

You provide external documentation by printing a copy of the procedure’s code; you can do so by right-clicking the project’s name in the Project Explorer window, and then clicking Print on the shortcut menu

You also can use the Print command on the Visual Basic Editor’s File menu, or you can press Ctrl+P

1

Page 26: Using the Visual Basic Editor Visual Basic for Applications 1.

Summary

To open the Visual Basic Editor: Click Tools on the host application’s menu bar,

point to Macro, and then click Visual Basic Editor

To open the Project Explorer window, which contains a listing of the open projects and their components:

Click View on the Visual Basic Editor menu bar and then click Project Explorer

To open the Properties window, which contains a listing of properties along with their default values:

Click View on the Visual Basic Editor menu bar, and then click Properties Window

1

Page 27: Using the Visual Basic Editor Visual Basic for Applications 1.

Summary

To view the Standard toolbar in the Visual Basic Editor main window:

Click View on the Visual Basic Editor menu bar, point to Toolbars, and then click Standard

To control the display of the items in the Project Explorer window:

Click the Toggle Folders button, as well asthe minus and plus signs, in the ProjectExplorer window

To display an object: Click the object’s name in the Project Explorer

window, and then click the View Object button

1

Page 28: Using the Visual Basic Editor Visual Basic for Applications 1.

Summary

To open an object’s Code window: Click the object’s name in the Project Explorer

window and then click the View Code buttonTo have the Visual Basic Editor assist you when

entering instructions in the Code window: Click Tools on the Visual Basic Editor menu bar

and then click OptionsTo document a procedure: Provide internal documentation by entering

comments in the Code window Provide external documentation by printing a copy

of the procedure’s code

1

Page 29: Using the Visual Basic Editor Visual Basic for Applications 1.

Summary

To display each procedure in a separateCode window:

Click the Procedure View button in theCode window

To display an object’s code as a single listing in the Code window:

Click the Full Module View button in theCode window

To save a procedure: You save a procedure by saving the file that

contains the procedure

1

Page 30: Using the Visual Basic Editor Visual Basic for Applications 1.

Summary

To run a procedure in order to verify the accuracy of its instructions:

While in the Visual Basic Editor, you can use either the Run menu or the Tools menu to run a procedure; you also can use the Run procedureType button on the Standard toolbar or you can use the F5 key

To print a procedure: Display the Print dialog box by using the Print

command on the Visual Basic Editor’s File menu, or by pressing Ctrl+P, or right-clicking the object’s name in the Project Explorer window and then clicking Print on the shortcut menu

1

Page 31: Using the Visual Basic Editor Visual Basic for Applications 1.

Excel Lesson:Using the Visual Basic Editor in

Microsoft Excel To open

the Visual Basic Editor in Excel refer to the steps on pages 29 to 32 of the textbook

1

Page 32: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor Opened in Excel

1

Page 33: Using the Visual Basic Editor Visual Basic for Applications 1.

Using the Visual Basic Editor in Microsoft Excel

To view the status of the Options dialog box use the steps on pages 32 and 33of the textbook

1

Page 34: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Workbook Object’s Open Event Procedure

To code the workbook’s Open event procedure use the steps on pages 33 to 36 of the textbook

1

Page 35: Using the Visual Basic Editor Visual Basic for Applications 1.

Code Window Showing the Completed Open Event Procedure

1

Page 36: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Workbook Object’s Open Event Procedure

To save and run the workbook’s Open event procedure use the steps on pages 36 and 37of the textbook

1

Page 37: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the GetGeniusMacro Procedure

A macro is a procedure that the user can run from the Macro dialog box in Excel

Before you can create a macro, you first must insert a Module object into the current project

To insert a Module object into the VBA Project (ToDo.xls) project, use the steps on page 38 ofthe textbook

To insert the GetGenius procedure into the Module 1 module, then code the procedure, use the steps on pages 38 to 40 of the textbook

1

Page 38: Using the Visual Basic Editor Visual Basic for Applications 1.

Add Procedure Dialog Box1

Page 39: Using the Visual Basic Editor Visual Basic for Applications 1.

Code Window Showing the Completed GetGenius Procedure

1

Page 40: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the GetGeniusMacro Procedure

To save and run the GetGenius procedure, then print the project’s code, usethe steps on pages 41 and 42 of the textbook

1

Page 41: Using the Visual Basic Editor Visual Basic for Applications 1.

Print Dialog Box1

Page 42: Using the Visual Basic Editor Visual Basic for Applications 1.

Word Lesson:Using the Visual Basic Editor in

Microsoft Word To open

the Visual Basic Editor in Word, use the steps on pages 45 to 47of the textbook

1

Page 43: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic Editor Opened in Word

1

Page 44: Using the Visual Basic Editor Visual Basic for Applications 1.

Using the Visual Basic Editor in Microsoft Word

Before opening a Code window and entering

the appropriate VBA instructions, you will

open the Visual Basic Editor’s Options dialog

box to verify that the Auto List Members and

Auto Quick Info check boxes are selected

To view the status of the Options dialog box

use the steps on page 49

1

Page 45: Using the Visual Basic Editor Visual Basic for Applications 1.

Options Dialog Box1

Page 46: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Document Object’s Open Event Procedure

You will code the Open event procedure so that it displays the Office Assistant and positions it in the upper-right corner of the screen

To code the document object’s Open event procedure, use the steps on pages 50 to 52 of the textbook

1

Page 47: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Document Object’s Open Event Procedure

1

To save and run the document’s Open event procedure, use the steps on pages 53 to 54 of the textbook

Page 48: Using the Visual Basic Editor Visual Basic for Applications 1.

Office Assistant Displayed in the Upper-Right Corner of the Screen

1

Page 49: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the DisplayDateMacro Procedure

Unlike the Open event procedure, which runs automatically when someone opens the New Member document, you want to be able to run the DisplayDate procedure whenever you choose to do so

For that to happen, the DisplayDate procedure will need to be a macro procedure

To insert a Module object into the Project (New Member) project, use the steps on pages 54 and 55 in the textbook

Then to insert the DisplayDate procedure into the Module1 module, then code the procedure, use the steps on pages 55 to 57 of the textbook

1

Page 50: Using the Visual Basic Editor Visual Basic for Applications 1.

Add Procedure Dialog Box1

Page 51: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the DisplayDateMacro Procedure

To save and run the DisplayDate procedure, and then print the project’s code, use the steps on pages 57 to 59 of the textbook

1

Page 52: Using the Visual Basic Editor Visual Basic for Applications 1.

New Member Document Showing the Current Date Displayed in a Dialog Box

1

Page 53: Using the Visual Basic Editor Visual Basic for Applications 1.

Print Dialog Box1

Page 54: Using the Visual Basic Editor Visual Basic for Applications 1.

Access Lesson:Using the Visual Basic Editor

in Microsoft Access As you learned in the Concept lesson, you

can use VBA to customize an application to fit your needs

You do so by creating a procedure, which is simply a series of VBA instructions grouped together as a single unit for the purpose of performing a specific task

To open the Visual Basic Editor inAccess, use the steps on pages 62 to 64of the textbook

1

Page 55: Using the Visual Basic Editor Visual Basic for Applications 1.

Database Window and the Clippit Office Assistant

1

Page 56: Using the Visual Basic Editor Visual Basic for Applications 1.

The Visual Basic EditorOpened in Access

1

Page 57: Using the Visual Basic Editor Visual Basic for Applications 1.

Using the Visual Basic Editor in Microsoft Access

Before opening a Code window and entering the appropriate VBA instructions, you will open the Visual Basic Editor’s Options dialog box to verify that the Auto List Members and Auto Quick Info check boxes are selected

To view the status of the Options dialog box, use the steps on pages 64 and 65 of the textbook

1

Page 58: Using the Visual Basic Editor Visual Basic for Applications 1.

Options Dialog Box1

Page 59: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Form Object’s Close Event Procedure

An event procedure runs in response to an action performed on an object by the user

A form’s Close event procedure runs automatically when the user closesthe form

To include the form’s name in the Project Explorer window, and then code its Close event procedure, use the steps on pages 65 to 70 of the textbook

1

Page 60: Using the Visual Basic Editor Visual Basic for Applications 1.

StudentForm Opened inDesign View

1

Page 61: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Form Object’s Close Event Procedure

1

Page 62: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Form Object’sClose Event Procedure

1

Page 63: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the Form Object’sClose Event Procedure

To save and run the form’s Close event procedure use the steps on pages 70 and 71 of the textbook

1

Page 64: Using the Visual Basic Editor Visual Basic for Applications 1.

Coding the CancelAssistantMacro Procedure

The way you create a macro in Access differs from the way you create a macro in the other Office 2000 applications

To create a macro in Access, usethe steps on pages 72 to 74 of the textbook

1

Page 65: Using the Visual Basic Editor Visual Basic for Applications 1.

MsgBox Action Added to the Macro Windows

1

Page 66: Using the Visual Basic Editor Visual Basic for Applications 1.

Creating a FunctionProcedure in Access

You may want a macro to perform a task for which no action exists in the Macro window’s Action list

In those cases, you need to use the Visual Basic Editor to create a function procedure, and then you use the Action list’s RunCode action to include the procedure in the macro

The RunCode action tells the macro to run the code contained in the function procedure

To insert a Module object into the Econ100 (Econ100) project, use the steps on page 74 ofthe textbook

1

Page 67: Using the Visual Basic Editor Visual Basic for Applications 1.

Creating a FunctionProcedure in Access

To insert the CancelAssistant function procedure into the Module1 module, then code the procedure, use the steps on pages 75 to 77 of the textbook

1

Page 68: Using the Visual Basic Editor Visual Basic for Applications 1.

Code Window Showingthe Completed

CancelAssistant Procedure

1

Page 69: Using the Visual Basic Editor Visual Basic for Applications 1.

Creating a FunctionProcedure in Access

To return to the Macro window and complete the CancelAssistantMacro macro, then save and run the macro, use the steps on pages 77 and 78 of the textbook

1


Recommended