VBA problem
MsgBoxInputBoxBasic
Variations
| Changing PVCalculator to account for missing input or cancel button depressed. Repeats input box, Exit if wrong type of data | ||||||||||||||||
| 2 | Sub PvCalculatorCancel() | |||||||||||||||
| Dim CF As Variant | ||||||||||||||||
| Exits sub if no CF entered or Canceled pressed | ||||||||||||||||
| On Error GoTo NotValidInput 'if no data is entered, or entered wrongly, error message given | ||||||||||||||||
| 1 | Sub PVCalculatorExit() | |||||||||||||||
| Dim CF | Do Until CF <> "" | |||||||||||||||
| CF = InputBox("Enter the cash flow value please", "PV calculator", "100") | CF = InputBox("Enter the cash flow value please", "PV calculator") | |||||||||||||||
| If CF = "" Then Exit Sub | Loop | |||||||||||||||
| MsgBox "The present value of: " & CF & _ | ||||||||||||||||
| " at 5% for 10 periods is: " & _ | MsgBox "The present value of: " & CF & _ | |||||||||||||||
| Round(Application.PV(0.05, 10, -CF), 2), _ | " at 5% for 10 periods is: " & _ | |||||||||||||||
| vbInformation, "PV calculator" | Round(Application.PV(0.05, 10, -CF), 2), _ | |||||||||||||||
| End Sub | vbInformation, "PV calculator" | |||||||||||||||
| OR | Exit Sub | |||||||||||||||
| If CF = "" Then | NotValidInput: | |||||||||||||||
| MsgBox "Cancel Pressed or no Cash flow entered Exiting", vbCritical, _ | MsgBox ("You entered invalid data. Please run program again and enter Cash flows in $ for each question.") | |||||||||||||||
| "New PV Calc" 'if nothing is entered in as a cash flow then a message pops up that the sub will be cancelled | End Sub | |||||||||||||||
| Exit Sub | ||||||||||||||||
| End If |
Will cancel if wrong input
Will repeat Input Box if left blank
Homework
| 6 possible points | ||||
| 3. Use Select Case to assign Grade | 1. Create a UDF which will assign an average homework grade percentage and corresponding letter grade. | |||
| Percentage of points | Letter Grade | Assume that 4 homeworks have been submitted: 8 of 10; 7 of 10; 9 of 10; 6 of 10. | ||
| 92-100 | A | |||
| 90-91 | A- | |||
| 88-89 | B+ | |||
| 80-87 | B | |||
| 78-79 | B- | |||
| 76-77 | C+ | |||
| 70-75 | C | |||
| 68-69 | C- | |||
| 60-67 | D | |||
| < 60 | F |
Sheet1
Practice finding error
| Sub findEffor() | |||||||||||||||
| Dim hours | Copy basic model | ||||||||||||||
| Dim wages | |||||||||||||||
| hours = InputBox("Enter number of hours", "Practice finding input errors", "1") | |||||||||||||||
| wages = hours * 10 | |||||||||||||||
| MsgBox "Your pay is " & wages | |||||||||||||||
| Add code that will result in exit if a mistake is made | |||||||||||||||
| Add code that will exit if non numeric entry, but will repeat question if field is left blank |
1. Write a version of the present-value calculator macro which asks for the cash flow value, the interest rate, and the number of periods. Then use a message box to display the answer. Practice your looping skills and find the PV by: PV(CF,r,n) = ∑CFt/(1+r)t
2. Now add code such that the routine asks for the parameter value again if the entry is blank. Hint: one possible approach is to use a Do Until the CF is not equal to “”
1. Write a version of the present -value calculator macro which asks for the cash flow value, the
interest rate, and the number of periods. Then use a message box to display the answer.
Practice your looping skills and find the PV by: PV(CF,r,n) = ∑CF
t
/(1+r)
t
2. Now add code such that the routine asks for the parameter value again if the entry is blank.
Hint: one possible approach is to use a Do Until the CF is not equal to “”