Download presentation
Presentation is loading. Please wait.
Published byArlene Rusten Modified over 9 years ago
1
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft Excel
2
Exploring Microsoft Excel 2002 Chapter 9 Objectives (1 of 2) Explain the importance of data validation and flexibility in a spreadsheet model Describe the YEAR, MONTH, DAY, and DATE functions Distinguish between the PMT, IPMT, and PPMT functions Describe the logical functions AND and OR Describe the Excel object model; define the terms collection, method, and property Describe how the macro recorder can be used to jump start VBA procedures
3
Exploring Microsoft Excel 2002 Chapter 9 Objectives (2 of 2) Distinguish between event procedures and general procedures Develop a custom toolbar and attach it to a workbook Describe a variable print area; explain how to set and clear the print area Create a user form for a splash screen Explain how pseudocode is used to develop more complex procedures
4
Exploring Microsoft Excel 2002 Chapter 9 Application Development 1.Determine the requirements Gather requirements from client Suggest additional appropriate features Data validation is an implied requirement 2.Design the interface Separate data input area from output area Build data validation into the interface Anticipate and prevent data entry errors Spreadsheet should be visually appealing
5
Exploring Microsoft Excel 2002 Chapter 9 Application Development 3.Develop the spreadsheet Spreadsheet must be flexible - a change in the data input area should flow throughout the spreadsheet Application must be bulletproof - formulas should work under all valid input parameters 4.Test and debug the spreadsheet Test to see that formulas are correct Try to make the application “crash”
6
Exploring Microsoft Excel 2002 Chapter 9 Application Development 5.Add automation as necessary Use macros to automate repetitive keystrokes and mouse clicks Use VBA to implement loops, decision making, and other repetitive tasks 6.Test and debug completed application Continue testing all spreadsheet formulas Test VBA procedures The secret of good testing is to try to “break” the application!
7
Exploring Microsoft Excel 2002 Chapter 9 Logical Functions Return one of two values, either true or false In the AND function, all arguments must be true for the function to be true In the OR function, only one argument must be true for the function to be true
8
Exploring Microsoft Excel 2002 Chapter 9 Date Functions Excel stores a date as a serial integer Common Date Functions: TODAY() returns the current date DATE() creates a date from three arguments: year, month, and day DAY() returns the numeric day from a date MONTH() returns the numeric month from a date YEAR() returns the numeric year from a date
9
Exploring Microsoft Excel 2002 Chapter 9 The Amortization Schedule Loan term has been entered Payments are now visible (loan term filled in) First payment is on the 16 th ; all subsequent payments occur on the 16 th of each month Figure 9.1b
10
Exploring Microsoft Excel 2002 Chapter 9 Date Functions Figure 9.3d Date() function has three arguments Month(B3)+1 returns the month of the date in cell B3 (8) + 1, giving you 9 or September
11
Exploring Microsoft Excel 2002 Chapter 9 Hands-On Exercise 1 Objective: Explore the amortization workbook with respect to data validation, cell protection, optional extra payments, and a variable print area Data Entry and Validation A Protected Worksheet The Logical AND Function Check the Calculations What If The Print Preview Command Print the Cell Formulas
12
Exploring Microsoft Excel 2002 Chapter 9 Exploring VBA Syntax (1 of 2) VBA accomplishes its tasks by manipulating objects Objects include workbooks, worksheets, charts, and cells The object model describes the way different objects are related to each other Collection: a group of similar objects The worksheet collection is a group of worksheets
13
Exploring Microsoft Excel 2002 Chapter 9 Exploring VBA Syntax (2 of 2) Method: an action performed by an object specify the object and the action to be performed, for example, Range(“Principal”).Select Property: an attribute of an object specify the object, the property, and the value, for example, Range(“Principal”).Bold = True
14
Exploring Microsoft Excel 2002 Chapter 9 Procedures Procedures are VBA code statements that perform a specific task Can be created in one of two ways: Entering the code directly in the VBA editor Using the macro recorder to capture Excel commands and convert them to VBA The real power of VBA lies in the ability to add VBA statements that have no Excel equivalent
15
Exploring Microsoft Excel 2002 Chapter 9 Functions Used in the Amortization Workbook PPMT: computes the amount of principal for a loan payment IPMT: computes the amount of interest for a loan payment Every penny of a loan payment is either applied to interest or reduces principal Keep it simple: use one function, then subtract that from the amount of the loan payment to compute the other
16
Exploring Microsoft Excel 2002 Chapter 9 The IPMT and PPMT Functions The IPMT function returns the amount of interest in a loan payment The PPMT function returns the amount of principal reduction in a loan payment
17
Exploring Microsoft Excel 2002 Chapter 9 Hands-On Exercise 2 Objective: To use the Excel macro recorder to jump-start the creation of VBA procedures; to add VBA statements to create more powerful procedures. Open the VBA Editor Record the Display Validation Columns Macro Create the Hide Validation Columns Macro Complete the Display Validation Columns Procedure Create the Enable Single Payments Procedure Test the Procedure Create a Custom Toolbar Test the Toolbar Button Attach the Toolbar
18
Exploring Microsoft Excel 2002 Chapter 9 Using the Macro Recorder Sub DisplayValidationColumns() ActiveSheet.Unprotect Application.Goto Reference:="ValidationColumns" Selection.EntireColumn.Hidden = False ActiveSheet.Protect End Sub Object Name and Method Property and Value Figure 9.4(a) Display Validation Columns (Macro Recorder)
19
Exploring Microsoft Excel 2002 Chapter 9 Adding Additional VBA Statements Sub DisplayValidationColumns() ActiveSheet.Unprotect If Range("DataEntered").Value = True Then Application.Goto Reference:="ValidationColumns" Selection.EntireColumn.Hidden = False MsgBox "This macro displays additional columns for validation " _ & "of the spreadsheet formulas using the IPMT and PPMT " _ & "functions, respectively. The values should match the " _ & "corresponding columns in the body of the spreadsheet " _ & "provided that no extra payments are made. ", _ vbInformation, ApplicationTitle Else MsgBox "Validation meaningless - Loan parameters not entered", _ vbInformation, ApplicationTitle End If ActiveSheet.Protect End Sub Figure 9.4(b) Display Validation Columns (Additional VBA Statements) MsgBox statements are different depending on whether the If condition is true or false Message box provides userful information to the user
20
Exploring Microsoft Excel 2002 Chapter 9 Event Procedures General Procedures Executed when the user explicitly chooses Examples include running a macro Event Procedures Executed when a specific event occurs An event is any action that an application recognizes Examples include clicking a command button
21
Exploring Microsoft Excel 2002 Chapter 9 Using Event –Driven Programming Decide which events occur and what should happen when they occur Common events: Open Workbook event occurs whenever a workbook is opened Use to display splash screens, attach custom toolbars or macro workbooks Before Close event occurs just before a workbook is closed Very helpful, since a workbook can be closed from the File menu, or by clicking the Close button.
22
Exploring Microsoft Excel 2002 Chapter 9 A Splash Screen A single form that appears for a brief period, then disappears Often used to identify the application and the developer. Can include copyright information, if released for distribution to the general public. Frequently included in the Open workbook event procedure
23
Exploring Microsoft Excel 2002 Chapter 9 A Splash Screen Splash Screen contains the name of the application and copyright information. Can also contain developer’s name and, if applicable, company Custom toolbar Figure 9.6(a) The Open Workbook Event Procedure
24
Exploring Microsoft Excel 2002 Chapter 9 The Open Workbook procedure Attach custom toolbar, then show UserForm1 (the splash screen) Take the user to the first input cell, reducing the chance for error Procedure executes whenever the workbook is opened. Figure 9.8(b) The Open Workbook Event Procedure (Code Window)
25
Exploring Microsoft Excel 2002 Chapter 9 User Forms Two common uses in Excel Data entry: you would write code to paste the values in the form into the input area(s) of the worksheet(s). Helpful if input area is large Display and then hide a splash screen
26
Exploring Microsoft Excel 2002 Chapter 9 Creating a User Form The Activate procedure displays form for 5 seconds After 5 seconds, call the CloseScreen procedure Figure 9.7 Creating a User Formc
27
Exploring Microsoft Excel 2002 Chapter 9 Hands-on Exercise 3 Objective: To create an Open workbook event procedure that displays a splash screen a custom toolbar; to create a Before Close event procedure to close the custom toolbar and display a message Start the Macro Recorder Create the Open Workbook Event Procedure Create the Before Close Event Procedure Test the Procedures Create the User Form Modify the Form Properties Create the Code for the User Form Test the Splash Screen
28
Exploring Microsoft Excel 2002 Chapter 9 More Complex Procedures VBA coding is not difficult You MUST understand the task you are trying to accomplish and the underlying logic before you can write the code Tasks are generally a set of smaller tasks that are performed together The sequence the tasks are performed in is often critical This is the case for VBA, Java, COBOL or any other programming language
29
Exploring Microsoft Excel 2002 Chapter 9 Define the Problem Carefully Objective: to apply an annual extra payment until the loan is paid off Generally requires breaking down into smaller steps: 1. Ask the user for the amount of the extra payment 2. Ask the user when the extra payments are to begin 3. Determine where in the table the payments are to begin 4. Apply the extra payments annually until the loan is paid off
30
Exploring Microsoft Excel 2002 Chapter 9 Pseudocode Pseudocode “neat notes to oneself” the most common way of expressing logic Think in terms of programming building blocks Sequence Decision making Loops Not written in any programming language Focus on the logic, not the syntax Time spent developing good pseudocode will save you much coding time! A non-programmer should be able to tell what your procedure does by reading the pseudocode
31
Exploring Microsoft Excel 2002 Chapter 9 Pseudocode… Input the amount of extra payment and date extra payments are to begin Set active cell to first table date Do Until active cell >= Date that payments are to begin Check next date Loop Move active cell to the extra payment column in this row Payoff Made = “No” Do Until Payoff Made = “Yes” Apply extra payment here If Ending Balance at end of year <= 0 Payoff Made = “Yes” Else Drop active cell 12 rows (1 year) End If Loop Display summary area Pseudocode is not written in any programming language; named ranges, procedures, etc. are not named Logic is easier to develop when you’re not focusing on syntax
32
…becomes code Sub PeriodicExtraPayment() Dim DateExtraPaymentsBegin As Date Dim AmountOfExtraPayment As Currency Dim PayoffMade As String AmountOfExtraPayment = InputBox("Enter amount of extra payment") DateExtraPaymentsBegin = InputBox("Enter date when payments begin") Application.Goto Reference:="FirstTableDate" Do Until ActiveCell >= DateExtraPaymentsBegin ActiveCell.Offset(1, 0).Select Loop ActiveCell.Offset(0, 4).Range("A1").Select PayoffMade = "No" Do Until PayoffMade = "Yes" ActiveCell = AmountOfExtraPayment If ActiveCell.Offset(11, 1) <= 0 Then 'Check one year later PayoffMade = "Yes" Else ActiveCell.Offset(12, 0).Select 'New payment necessary End If Loop Range("TotalOfExtraPayments").Select End Sub Declare variables, set equal to values entered in Input Boxes Don’t forget to comment your code. Comments are in green Loop down one row until the value in the cell is >= the value entered in the Input Box Use a “switch” to control program action
33
Exploring Microsoft Excel 2002 Chapter 9 Hands-on Exercise 4 Objective: Develop a procedure to apply periodic extra payments to a loan until the loan is paid off Start the Macro Recorder View and Add Code Step Through the Procedure Add Additional Code Test the Procedure Revise the Procedure Retest the Procedure
34
Exploring Microsoft Excel 2002 Chapter 9 Summary (1 of 2) Applications written for users who may not be proficient in Excel Characteristics of a good application visually appealing includes good data validation enables user to do things he/she could not do without benefit of VBA Date functions: TODAY, DATE, DAY, MONTH, YEAR VBA accomplishes its tasks by manipulating objects (workbooks, cells, charts. etc.)
35
Exploring Microsoft Excel 2002 Chapter 9 Summary (2 of 2) Events are any actions that are recognized by an application VBA is an event-driven language Use custom toolbars as a convenient way to run applications User forms are a convenient way to enter data or display splash screens Use pseudocode to develop the logic for a procedure before writing the code
36
Exploring Microsoft Excel 2002 Chapter 9 The Amortization Schedule Application Figure 9.16 The Completed Application (Exercise 6)
37
Exploring Microsoft Excel 2002 Chapter 9 The Get Out of Debt Application Figure 9.20 The Completed Application (Exercise 10)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.