me 142 engineering computation i input, output & documentation
TRANSCRIPT
ME 142Engineering Computation I
Input, Output & Documentation
Key Concepts
Documentation
Getting Program Input and Returning Program Output
Linking a Program to a Button
Header 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
Improving Program Readability
Include Header Use descriptive variable names Use indentation and blank lines to clarify
structure Add other comments as appropriate
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
Getting 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 cellsSub MyPgm()
…
End Sub
Using 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
Using 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
Using the Cells command to Read/Write Spreadsheet Data
Example:
Sub Square()
A=cells(2,1)
B=A^2
cells(2,2)=B
End Sub
Linking a Program to a Button
InputBox Function
Used to obtain a single value from the userMyVar = 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)
MsgBox Function
Examples
Ans = MsgBox("Continue processing?", vbYesNo)
MsgBox "Click OK to begin printing"
Common MsgBox Function Constants
Constant Name End ResultvbOKOnlyvbOKCancelvbAbortRetryIgnorevbYesNoCancelvbYesNovbRetryCancel
Displays OK Button onlyDisplays OK and Cancel buttonsDisplays Abort, Retry, and Ignore buttonsDisplays Yes, No, and Cancel buttonsDisplays Yes and No buttonsDisplays Retry and Cancel buttons