ME 142 Engineering Computation I Interacting with Spreadsheets
Key Concepts Getting Input from a Message Box Returning Program Output from a Message Box
Getting Input/Returning Output Via Functions Passing Information to a Function via an Argument List Returning Results from a Function to a Cell Direct to/from a Spreadsheet Reading Data from a Spreadsheet Cell Returning Data to a Cell Dialog Boxes InputBox Function MsgBox Function
InputBox Function
Used to obtain a single value from the user MyVar = InputBox(prompt [, title] [,default]) Where Prompt – is the text displayed in the input box (required) Title – text displayed in the input box’s title bar (optional) Default – defines the default value (optional) MyVar – variable which will receive the input entered
InputBox Function Examples Name = InputBox("Please enter your name: ","Name") MyNum = Val(InputBox("Enter the height:","Height"))
MsgBox Function
Used to display information and get simple user input [MyVar =] MsgBox(prompt [, buttons] [,title]) Where Prompt – is the text displayed in the message box (required) Buttons – specifies buttons/icons appear in the message box (optional) Title – specifies the text displayed in the input box’s title bar (optional) MyVar – variable receiving value of mouse click button (optional)
Example Problem Write a VBA program to calculate the surface area of a rectangular prism given the length, width, and height: As a Function, using the argument list As a Sub, reading from/writing to the spreadsheet As a Sub, using dialog boxes Modify the programs to also calculate the volume
MsgBox Function Examples MsgBox “Click OK to begin printing” Ans = MsgBox("Continue processing?", 4)
Values Used in MsgBox Function ValueConstant NameEnd Result vbOKOnly vbOKCancel vbAbortRetryIgnore vbYesNoCancel vbYesNo vbRetryCancel vbCritical vbQuestion vbExclamation vbInformation vbDefaultButton1 vbDefaultButton2 vbDefaultButton3 vbDefaultButton4 Displays OK Button only Displays OK and Cancel buttons Displays Abort, Retry, and Ignore buttons Displays Yes, No, and Cancel buttons Displays Yes and No buttons Displays Retry and Cancel buttons Displays Critical Message icon Displays Warning Query Icon Displays Warning Message icon Displays Information Message icon First button is default Second button is default Third button is default Fourth button is default
Values Returned by the MsgBox Function ValueConstant NameMeaning vbOK vbCancel vbAbort vbRetry vbIgnore vbYes vbNo User clicked OK User clicked Cancel User clicked Abort User clicked Retry User clicked Ignore User clicked Yes User clicked No
MsgBox Function Example: Display Yes/No buttons with exclamation point icon, default of no Button = vbYesNo +vbExclamation + vbDefaultButton2 Ans = MsgBox("Continue Processing?",Button)
Review Questions
Review Question InputBox Function What type of data does the InputBox function return? A.Integer B.Real C.String D.May return any of the above
Homework Help ‘n Hints