Download presentation
Presentation is loading. Please wait.
Published byAdrian Lyons Modified over 9 years ago
1
ME 142 Engineering Computation I Input, Output & Documentation
2
Key Concepts Documentation Getting Program Input and Returning Program Output Linking a Program to a Button
3
Documentation
4
What is a Header?
5
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
6
Improving Program Readability Include Header Use descriptive variable names Use indentation and blank lines to clarify structure Add other comments as appropriate
7
Program Input & Output
8
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 cells Sub MyPgm() … End Sub
9
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
10
Function Example Function AddMe(a,b) ‘ This functions adds two numbers, a and b ‘compute sum c = a+b 'output results AddMe = c End Function
11
Reading Data from a Cell Example A=Cells(2,1) MsgBox ("Cell value is " & A) A = Range(“A2”)
12
Writing Data to a Cell Example c=sqr(3^2+4^2) Cells(2,3)=c Range(“C2”)=c
13
Example Problem Write a VBA program to calculate the surface area and volume 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 Modify the programs to also calculate the volume
14
Reading Data from a Cell Example Sheets("Sheet1").Select Range("A2").Select A = ActiveCell.Value B = ActiveCell.Offset(0, 1).Value ActiveCell.Offset(0, 1).Select C = A + B MsgBox ("C = " & C) What does Offset do? Cells(2,1).Select
15
Reading Data from a Cell Example Sheets("Sheet1").Select Range("A2").Select A = ActiveCell.Value B = ActiveCell.Offset(0, 1).Value ActiveCell.Offset(0, 1).Select C = A + B MsgBox ("C = " & C) What would happen if I omitted Range(“A2”).Select?
16
Writing Data to a Cell Example c=sqr(3^2+4^2) Range("c2").Select ActiveCell.Value = c
17
Linking a Program to a Button
19
How Do You Put “Help” Strings in Functions you Create? 1.Create function 2.Navigate to function in Object Browser 3.Right click and select Properties 4.Enter Help text 5.To view Help, click on f x button in formula bar after entering function and first parenthesis Note: This method is not dynamic like with Excel’s built-in functions, but is a reasonable substitute
20
Review Questions
21
Review Question Which of the following correctly reads the contents of cell A2 and places it in variable X? A.X=Cells(2,1) B.X=Cells(1,2) C.Cells(2,1)=X D.Cells(2,1)=X
22
Homework Help ‘n Hints
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.