occ network drives h:\ p:\
TRANSCRIPT
OCC Network Drives H:\
P:\
https://myfiles.oakton.edu/
Introducing the Visual Basic Editor(Unit 1)
Visual Basic for Applications
Objectives In this unit, you’ll 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
Display a message in a dialog box
Run, save, and print a procedure
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
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
Working with Macros in Office 2007 Enable developer tab
Adjust macros security setting
Save as PowerPoint Macro-Enabled Presentation
Powerpoint 2003 shortcut to create macros: ALT+T M R
PowerPoint Options
Macro Security Setting
Disable all macros with notification is safer
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
The Visual Basic Editor Opened in Microsoft Excel
Exhibit 1-1: The Visual Basic Editor opened in Microsoft Excel
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
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
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
Code Window Showing the MorningMsg Procedure
Exhibit 1-2: The Code window showing the MorningMsg procedure
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
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
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
You can use Full Module View to view procedures in the code window as a single, scrollable list, or you can use Procedure View to view one procedure at a time.
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
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 enable
Auto List Members
Auto Quick Info check boxes
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
Entering Instructions in the Code Window
Exhibit 1-3: The member list for the Application object
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
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 The term syntax refers to the rules of a programming language
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
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
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
SummaryTo open the Visual Basic Editor:
On the host application press Alt+F11, to open the VBE (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
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
To display an object: Click the object’s name in the Project Explorer window, and then click the
View Object button
Summary To open an object’s Code window:
Click the object’s name in the Project Explorer window and then click the View Code button
To 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 Options
To document a procedure: Provide internal documentation by entering comments in the Code window
Summary To display each procedure in a separate Code window:
Click the Procedure View button in the Code window
To display an object’s code as a single listing in the Code window:
Click the Full Module View button in the Code window
To save a procedure: You save a procedure by saving the file that contains the procedure
SummaryTo 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
Coding the Workbook Object’s Open Event Procedure
To save and run the workbook’s Open event procedure you need to open the workbook
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
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
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
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 closes the form
Coding a procedure 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