Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the Visual Basic Editor Visual Basic for Applications 1.

Similar presentations


Presentation on theme: "Using the Visual Basic Editor Visual Basic for Applications 1."— Presentation transcript:

1 Using the Visual Basic Editor Visual Basic for Applications 1

2 Objectives n In this tutorial, you will learn how to: n Recognize the components of the Visual Basic Editor n Enter code using the Visual Basic Editor n Create a procedure n Internally document a procedure n Manipulate the Office Assistant using code n Display a message in a dialog box n Run, save, and print a procedure 1

3 Concept Lesson: Procedures n Visual Basic for Applications (VBA) is the programming language found in many Microsoft and non-Microsoft products n You can use VBA to customize an application to fit your needs n 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

4 Procedures n You can run some procedures, called macros, directly from the Macros dialog box n Other procedures, called event procedures, run in response to specific actions you perform on an object n Those actions—such as opening a document, activating a worksheet, or clicking a command button—are called events 1

5 The Visual Basic Editor n The host application is the application in which you are working n The Visual Basic Editor contains three separate windows: – The main window – The Project Explorer window – The Properties window n Visual Basic Editor will have a similar interface in every application in which it is contained n The main window, at the top of the screen, contains the title bar, the menu bar, and the Standard toolbar 1

6 The Visual Basic Editor Opened in Microsoft Excel 1

7 The Visual Basic Editor n The Project Explorer window displays a list of the open projects and their components n A Module object is simply a container that stores macros and other procedures that are not associated with any specific object in the project n Every VBA object has a set of characteristics, called properties, associated with it that control the object’s appearance and behavior n These properties, along with their values, are listed in the Properties window 1

8 The Visual Basic Editor n The Toggle Folders button controls the display of the folders in the Project Explorer window n 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 n You can use the View Object button to view the object whose name is selected in the Project Explorer window n When an object is selected in the Project Explorer window, you can use the View Code button to open its Code window 1

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

10 Project Explorer Window Showing Minus and Plus Signs 1

11 The Visual Basic Editor n In the Code window, you enter the VBA instructions, called code, that instruct a procedure on how to perform a task n You enter your VBA instructions between the Public Sub and End Sub lines in the Code window n 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 n A keyword is a word that has a special meaning in a programming language 1

12 Code Window Showing the MorningMsg Procedure 1

13 The Visual Basic Editor n A sub procedure refers to a block of code that performs a specific task, but does not return a value n Function procedures, which are designated by the keyword Function, are procedures that can return a value n The keywords Public and Private indicate the procedure’s scope, which determines which objects can use the procedure n A Public scope indicates that the procedure can be used by all objects within the project 1

14 The Visual Basic Editor n A Private scope indicates that the procedure can be used only by the object in which the procedure is contained n The keyword End indicates the end of something n The `display message line is a comment n The Object list box either will display the word (General), or it will display the type of object associated with the Code window n The Procedure list box displays the name of the current procedure 1

15 The Visual Basic Editor 1

16 n Sometimes scroll bars will appear on the Procedure list box to indicate that not all of the procedures are currently displayed n In VBA the Assistant object represents the Office Assistant, and its FileName property controls the character used to display the Office Assistant n When the Procedure View button is selected, each procedure associated with the selected object appears in its own Code window n 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

17 Maximized Code Window with the Full Module View Button Selected 1

18 Entering Instructions in the Code Window n 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 n 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

19 Entering Instructions in the Code Window n A method is a predefined VBA procedure, which is simply a procedure that the Microsoft programmers have already coded for you n 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 n 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

20 Entering Instructions in the Code Window 1

21 n 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 n 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 n 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

22 Entering Instructions in the Code Window n The term syntax refers to the rules of a programming language n According to the syntax, you can include five items of information, called arguments, in the MsgBox command 1

23 Saving a Procedure n You save a VBA procedure by saving the file that contains the procedure n 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 n After saving a procedure, you then run it to verify that it is working correctly 1

24 Running a Procedure n While in the Visual Basic Editor, you can use either the Run menu or the Tools menu to run a procedure n You also click the Run procedureType button on the Standard toolbar or you can press the F5 key n The method you use to run a procedure from the host application depends on the type of procedure being run n 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

25 Printing a Procedure n 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 n You also can use the Print command on the Visual Basic Editor’s File menu, or you can press Ctrl+P 1

26 Summary To open the Visual Basic Editor: n 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: n 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: n Click View on the Visual Basic Editor menu bar, and then click Properties Window 1

27 Summary To view the Standard toolbar in the Visual Basic Editor main window: n 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: n Click the Toggle Folders button, as well as the minus and plus signs, in the Project Explorer window To display an object: n Click the object’s name in the Project Explorer window, and then click the View Object button 1

28 Summary To open an object’s Code window: n 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: n Click Tools on the Visual Basic Editor menu bar and then click Options To document a procedure: n Provide internal documentation by entering comments in the Code window n Provide external documentation by printing a copy of the procedure’s code 1

29 Summary To display each procedure in a separate Code window: n Click the Procedure View button in the Code window To display an object’s code as a single listing in the Code window: n Click the Full Module View button in the Code window To save a procedure: n You save a procedure by saving the file that contains the procedure 1

30 Summary To run a procedure in order to verify the accuracy of its instructions: n 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: n 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

31 Excel Lesson: Using the Visual Basic Editor in Microsoft Excel n To open the Visual Basic Editor in Excel refer to the steps on pages 29 to 32 of the textbook 1

32 The Visual Basic Editor Opened in Excel 1

33 Using the Visual Basic Editor in Microsoft Excel n To view the status of the Options dialog box use the steps on pages 32 and 33 of the textbook 1

34 Coding the Workbook Object’s Open Event Procedure n To code the workbook’s Open event procedure use the steps on pages 33 to 36 of the textbook 1

35 Code Window Showing the Completed Open Event Procedure 1

36 Coding the Workbook Object’s Open Event Procedure n To save and run the workbook’s Open event procedure use the steps on pages 36 and 37 of the textbook 1

37 Coding the GetGenius Macro Procedure n A macro is a procedure that the user can run from the Macro dialog box in Excel n Before you can create a macro, you first must insert a Module object into the current project n To insert a Module object into the VBA Project (ToDo.xls) project, use the steps on page 38 of the textbook n 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

38 Add Procedure Dialog Box 1

39 Code Window Showing the Completed GetGenius Procedure 1

40 Coding the GetGenius Macro Procedure n To save and run the GetGenius procedure, then print the project’s code, use the steps on pages 41 and 42 of the textbook 1

41 Print Dialog Box 1

42 Word Lesson: Using the Visual Basic Editor in Microsoft Word n To open the Visual Basic Editor in Word, use the steps on pages 45 to 47 of the textbook 1

43 The Visual Basic Editor Opened in Word 1

44 Using the Visual Basic Editor in Microsoft Word n 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 n To view the status of the Options dialog box use the steps on page 49 1

45 Options Dialog Box 1

46 Coding the Document Object’s Open Event Procedure n 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 n To code the document object’s Open event procedure, use the steps on pages 50 to 52 of the textbook 1

47 Coding the Document Object’s Open Event Procedure 1 n To save and run the document’s Open event procedure, use the steps on pages 53 to 54 of the textbook

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

49 Coding the DisplayDate Macro Procedure n 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 n For that to happen, the DisplayDate procedure will need to be a macro procedure n To insert a Module object into the Project (New Member) project, use the steps on pages 54 and 55 in the textbook n 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

50 Add Procedure Dialog Box 1

51 Coding the DisplayDate Macro Procedure n 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

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

53 Print Dialog Box 1

54 Access Lesson: Using the Visual Basic Editor in Microsoft Access n As you learned in the Concept lesson, you can use VBA to customize an application to fit your needs n 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 n To open the Visual Basic Editor in Access, use the steps on pages 62 to 64 of the textbook 1

55 Database Window and the Clippit Office Assistant 1

56 The Visual Basic Editor Opened in Access 1

57 Using the Visual Basic Editor in Microsoft Access n 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 n To view the status of the Options dialog box, use the steps on pages 64 and 65 of the textbook 1

58 Options Dialog Box 1

59 Coding the Form Object’s Close Event Procedure n An event procedure runs in response to an action performed on an object by the user n A form’s Close event procedure runs automatically when the user closes the form n 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

60 StudentForm Opened in Design View 1

61 Coding the Form Object’s Close Event Procedure 1

62 1

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

64 Coding the CancelAssistantMacro Procedure n The way you create a macro in Access differs from the way you create a macro in the other Office 2000 applications n To create a macro in Access, use the steps on pages 72 to 74 of the textbook 1

65 MsgBox Action Added to the Macro Windows 1

66 Creating a Function Procedure in Access n You may want a macro to perform a task for which no action exists in the Macro window’s Action list n 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 n The RunCode action tells the macro to run the code contained in the function procedure n To insert a Module object into the Econ100 (Econ100) project, use the steps on page 74 of the textbook 1

67 Creating a Function Procedure in Access n 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

68 Code Window Showing the Completed CancelAssistant Procedure 1

69 Creating a Function Procedure in Access n To return to the Macro window and complete the CancelAssistant Macro macro, then save and run the macro, use the steps on pages 77 and 78 of the textbook 1


Download ppt "Using the Visual Basic Editor Visual Basic for Applications 1."

Similar presentations


Ads by Google