Title: ME 142 Engineering Computation I
1ME 142Engineering Computation I
- Input, Output Documentation
2Key Concepts
- Documentation
- Getting Program Input and Returning Program
Output - Linking a Program to a Button
3Header Example
- Purpose
- This program computes the hypotenuse of a
- triangle, given the legs
- Input
- A, B legs of the triangle
- Output
- Hyp hypotenuse of the triangle
- Author GS Miller
- Date Created 10/15/2008
- Limitations None
4Improving Program Readability
- Include Header
- Use descriptive variable names
- Use indentation and blank lines to clarify
structure - Add other comments as appropriate
5Getting 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
6Getting Input/Returning Output
- VBA Supports 2 types of programs
- Functions
- Subprograms or Subroutines
- Functions typically receive all of their input
through the parameter list and write their output
to the cell from which the function was launched - Subprograms may read/write directly to/from cells
- Sub MyPgm()
-
- End Sub
7Using the Cells command to Read/Write Spreadsheet
Data
- Data may be read from a spreadsheet cell using
the cells command as shown below - Â variable Cells(row,col)
- Â row,column the address of the cell to be read
- variable name of variable which receives the
contents of the cell
8Using the Cells command to Read/Write Spreadsheet
Data
- Data may be written to a spreadsheet cell using
the cells command as shown below - Â Cells(row,col) variable
- Â row,column - the address of the cell to be
written - variable variable to be written to the cell
9Using the Cells command to Read/Write Spreadsheet
Data
- Example
- Sub Square()
- Acells(2,1)
- BA2
- cells(2,2)B
- End Sub
10Linking a Program to a Button
11InputBox 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 boxs title
bar (optional) - Default defines the default value (optional)
- MyVar variable which will receive the input
entered
12InputBox Function
- Examples
- Name InputBox("Please enter your name
","Name") -
- MyNum Val(InputBox("Enter the height",
"Height"))
13MsgBox 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 boxs title bar (optional) - MyVar variable receiving value of mouse click
button (optional)
14MsgBox Function
- Examples
- Ans MsgBox("Continue processing?", vbYesNo)
-
- MsgBox "Click OK to begin printing"
15Common MsgBox Function Constants
Constant Name End Result
vbOKOnly vbOKCancel vbAbortRetryIgnore vbYesNoCancel vbYesNo vbRetryCancel 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