ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming...

Post on 27-Dec-2015

214 views 0 download

Tags:

Transcript of ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming...

ME 142Engineering Computation I

Variables, Equations & Functions

Introduction to Basic Programming Language

Key Concepts

Defining Variables & Equations

Launching VBA

Programming Basics

Defining Variables & Equations

What is a Variable?

Variables

A named element that stores information or data Variable examples:

A=2.45 X=Y+Z I=I+1 B=“Hello Class”

Basic Math Operators

Same as used in Excel spreadsheets+ add

- subtract

* multiply

/ divide

( ) parenthesis

^ exponent

Launching VBA

Enabling the Developer’s Ribbon

Launching VBA

Enable Developer’s Tab Once enabled, should remain enabled

Select Visual Basic from Developer’s Tab

Launching VBA

Right click on Project…

Select Insert/Module

Launching VBA

Congratulations, you’re ready to begin programming!

Programming Basics

Example VBA Function

Function demo(a,b) ' (Input)'An example program to add two numbers

'Add 2 numbers (Process) c = a + b

'Return results (Output) demo= c End Function

Executing a VBA Function

Saving a VBA Program

How do you save a program? What is a macro-enabled workbook? Where is the program saved? How do I retrieve a program? What is this error about macros that I get

when I try to open my saved excel file?

Excel/VBA Tip

The VBA Function worksheetfunction may be used to call Excel function:

P=worksheetfunction.pi

D=worksheetfunction.degrees(x)

Example ProblemDevelop a VBA function to calculate the

volume of a sphere, given its radius, r. Volume = 4 * π * r3 / 3

Use your new VBA function within Excel to find the total volume of the metal used to make the cannonball pyramid below, given d=4.5”

Review Questions

I-Clicker QuestionVariables

Which of the following variable assignments are valid:

A. x = y + z

B. y + z = x

C. Both are valid variable assignments

D. Neither are valid variable assignments

I-Clicker QuestionLaunching VBA

The shortcut to launch the VBA editor is:A. Alt + V + B

B. F7

C. Alt + F11

D. None of the above

E. There is no shortcut

I-Clicker QuestionRunning VBA

In the first line of a function, the parenthesis immediately after the function name may be omitted:

A. True

B. False

Homework Help ‘n Hints