me 142 engineering computation i input, output & documentation

15
ME 142 Engineering Computation I Input, Output & Documentation

Upload: bennett-fisher

Post on 21-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ME 142 Engineering Computation I Input, Output & Documentation

ME 142Engineering Computation I

Input, Output & Documentation

Page 2: ME 142 Engineering Computation I Input, Output & Documentation

Key Concepts

Documentation

Getting Program Input and Returning Program Output

Linking a Program to a Button

Page 3: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 4: ME 142 Engineering Computation I Input, Output & Documentation

Improving Program Readability

Include Header Use descriptive variable names Use indentation and blank lines to clarify

structure Add other comments as appropriate

Page 5: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 6: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 7: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 8: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 9: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 10: ME 142 Engineering Computation I Input, Output & Documentation

Linking a Program to a Button

Page 11: ME 142 Engineering Computation I Input, Output & Documentation

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

Page 12: ME 142 Engineering Computation I Input, Output & Documentation

InputBox Function

Examples

Name = InputBox("Please enter your name: ","Name")

MyNum = Val(InputBox("Enter the height:", "Height"))

Page 13: ME 142 Engineering Computation I Input, Output & Documentation

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)

Page 14: ME 142 Engineering Computation I Input, Output & Documentation

MsgBox Function

Examples

Ans = MsgBox("Continue processing?", vbYesNo)

MsgBox "Click OK to begin printing"

Page 15: ME 142 Engineering Computation I Input, Output & Documentation

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