Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.

Similar presentations


Presentation on theme: "The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel."— Presentation transcript:

1 The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel 2003

2 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 2 12.1 Introducing the VBA Environment Microsoft Visual Basic for Applications (VBA) is the shared and fully integrated macro development environment available in Microsoft Office System 2003. Aside from its familial status in Office, VBA is itself an independent software program and you may find it included in other application software, such as AutoCAD and Microsoft Visio. VBA provides a subset of the features found in the Microsoft Visual Basic programming language. While Visual Basic creates stand- alone applications, VBA must be run within a host application like Microsoft Office Excel 2003.

3 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 3 12.1.1 Touring the Visual Basic Editor Figure 12.1 Executing a macro

4 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 4 12.1.1 Touring the Visual Basic Editor Figure 12.2 The Visual Basic Editor Properties window Work area Code window Project Explorer window

5 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 5 12.1.1 Touring the Visual Basic Editor Figure 12.3 Project Explorer window VBA code module object Project file for the “VBA Tour” workbook Microsoft Excel 2003 workbook and worksheet objects

6 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 6 12.1.1 Touring the Visual Basic Editor Figure 12.4 Properties window Select a property by clicking its name Click a tab to display the properties in alphabetical order or grouped by category Displays the name and type of the selected object Double-click a property box to set its value

7 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 7 12.1.1 Touring the Visual Basic Editor Figure 12.5 Code window View buttons Macro name Procedure box Object box

8 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 8 12.1.1 Touring the Visual Basic Editor Figure 12.6 Tiling Code windows in the Visual Basic Editor

9 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 9 12.1.2 Stepping Through a Macro Figure 12.7 Stepping through code in the Editor Margin Indicator bar The yellow arrow and code highlighting indicate the line to be executed next

10 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 10 12.1.3 Using Breakpoints Figure 12.8 Setting a breakpoint Margin Indicator bar The red circle and highlighting indicate that a breakpoint has been set

11 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 11 12.2 Understanding the VBA Language Figure 12.9 Sub procedure named “Company Name” Keywords appear blue among the program statements Comments appear green in the Code window and are preceded by an apostrophe The declaration line specifies the name and type of the procedure

12 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 12 12.2 Understanding the VBA Language Figure 12.10 Excel 2003’s Object Model, as displayed in the Editor’s help system

13 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 13 12.2 Understanding the VBA Language Figure 12.11 Declaring and assigning values to variables Assigning values to variables Declaring variables and data types

14 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 14 12.2.1 Writing VBA Code Figure 12.12 Adding a procedure to a module You must specify the type of procedure that you want to create The code module’s Name property has been changed to “MyCode”

15 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 15 12.2.1 Writing VBA Code Figure 12.13 Using the AutoList feature to enter code When you type a period in the With structure, the Editor displays a list of the properties and methods that apply to the object

16 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 16 12.2.1 Writing VBA Code Figure 12.14 The Toggle Display Sub procedure

17 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 17 12.2.1 Writing VBA Code Figure 12.15 Running the Toggle Display Sub Procedure The display of gridlines and row and column headings is toggled off

18 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 18 12.2.2 Working with Excel Objects Figure 12.16 Preparing the worksheet

19 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 19 12.2.2 Working with Excel Objects Figure 12.17 Maximizing the Code window Click the Procedure View button to view the active procedure only The Procedure box displays the active procedure’s name The insertion point is positioned in the active procedure

20 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 20 12.2.2 Working with Excel Objects Figure 12.18 Displaying the Application object’s AutoList window

21 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 21 12.2.2 Working with Excel Objects Figure 12.19 Completing the AppInfo Sub procedure

22 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 22 12.3 Controlling Your Procedures One of the greatest benefits of writing your own procedures is the ability to control how, when, and what code executes. VBA provides several control structures that allow you to test conditions for processing and to perform looping operations, which run the same lines of code repeatedly. These structures let you work within a dynamic environment, engage the user, and make processing decisions within your code. In this module, you are introduced to the most commonly used VBA control structures.

23 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 23 12.3.1 Making Decisions with IF…THEN Figure 12.20 Opening the EX1230 workbook

24 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 24 12.3.1 Making Decisions with IF…THEN Figure 12.21 Creating the “Calc Payment” Sub procedure The Code window is maximized in the work area Inserting a new code module into the project file A Sub procedure always begins with “Sub” and ends with “End Sub”

25 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 25 12.3.1 Making Decisions with IF…THEN Figure 12.22 The completed Calcpayment Sub procedure

26 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 26 12.3.2 Making Decisions with Select…Case Figure 12.23 Entering variable declarations

27 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 27 12.3.2 Making Decisions with Select…Case Figure 12.24 The completed CalcGrade Sub procedure

28 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 28 12.3.3 Looping with For…Next Figure 12.25 The Loop worksheet in the Control Structures workbook

29 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 29 12.3.3 Looping with For…Next Figure 12.26 Declaring variables and assigning values The concatenation operator is used to join the column letter with the total number of rows

30 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 30 12.3.3 Looping with For…Next Figure 12.27 The completed BoldTask Sub procedure The “c” represents each cell in the rTasks range object

31 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 31 12.3.4 Looping with Do…While Figure 12.28 Declaring variables and assigning values

32 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 32 12.3.4 Looping with Do…While Figure 12.29 The completed BoldJan Sub procedure

33 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 33 12.4 Interacting with The User A successful program creates and fosters a positive user experience and trust in the system. For some users, the black box approach of having workbook tasks and procedures running silently in the background is disconcerting. They appreciate knowing more about what is happening and being able to control execution or, at the very least, provide input. There are other times when a program requires user input or direction in order to continue processing. VBA provides two functions, MsgBox and InputBox, for use in programming user interaction. These functions enable you to display and collect information using built-in dialog box forms.

34 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 34 12.4.1 Using the MSGBOX Function Figure 12.30 Displaying a message box

35 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 35 12.4.1 Using the MSGBOX Function Figure 12.31 The completed “Display Message” Sub procedure

36 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 36 12.4.1 Using the MSGBOX Function Figure 12.32 Running the “Display Message” Sub procedure Concatenated expression msgTitle msgText msgButtons

37 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 37 12.4.2 Using the INPUTBOX Function Figure 12.33 Declaring variables and assigning values

38 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 38 12.4.2 Using the INPUTBOX Function Figure 12.34 The completed YearBorn Sub procedure

39 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 39 12.4.2 Using the INPUTBOX Function Figure 12.35 Running the YearBorn Sub procedure Assuming that the current year is 2004, this dialog box appears when “24” is entered

40 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 40 12.5 Working with Controls Like all of the Microsoft Office System 2003 applications, Excel 2003 provides full support for ActiveX controls. Formerly known as OLE controls, ActiveX controls are prebuilt, reusable software components, such as command buttons and drop-down list boxes, that you can use to add interface elements and functionality to your workbooks. You place these controls directly on a worksheet or display them in a custom user form. There are thousands of ActiveX controls, produced by Microsoft and third-party developers, that you can take advantage of in Excel 2003.

41 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 41 12.5.1 Placing a Command Button On the Worksheet Figure 12.36 Opening the EX1250 workbook

42 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 42 12.5.1 Placing a Command Button On the Worksheet Figure 12.37 Control Toolbox toolbar Design Mode View Mode Check Box Command Button List Box Properties Text Box Option Button Combo Box Spin Button Label Toggle Button Scroll Bar Image More Controls

43 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 43 12.5.1 Placing a Command Button On the Worksheet Figure 12.38 Inserting a command button control Displaying the Properties window for the selected control The Command Button ActiveX control is selected in Design mode

44 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 44 12.5.1 Placing a Command Button On the Worksheet Figure 12.39 Running a macro procedure in the command button’s Click event

45 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 45 12.5.1 Placing a Command Button On the Worksheet Figure 12.40 Running the macro procedure from the command button

46 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 46 12.5.2 Linking Controls and Cells Figure 12.41 Placing a combo box on the worksheet

47 ©2005 The McGraw-Hill Companies, Inc. All rights reserved 47 12.5.2 Linking Controls and Cells Figure 12.42 Testing a combo box control Values displayed in this combo box control are stored in the MonthNames range on the Lists worksheet


Download ppt "The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel."

Similar presentations


Ads by Google