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

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

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

Page 1: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

ME 142Engineering Computation I

Variables, Equations & Functions

Introduction to Basic Programming Language

Page 2: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Key Concepts

Defining Variables & Equations

Launching VBA

Programming Basics

Page 3: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Defining Variables & Equations

Page 4: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

What is a Variable?

Page 5: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Variables

A named element that stores information or data Variable examples:

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

Page 6: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Basic Math Operators

Same as used in Excel spreadsheets+ add

- subtract

* multiply

/ divide

( ) parenthesis

^ exponent

Page 7: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Launching VBA

Page 8: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Enabling the Developer’s Ribbon

Page 9: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Launching VBA

Enable Developer’s Tab Once enabled, should remain enabled

Select Visual Basic from Developer’s Tab

Page 10: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Launching VBA

Right click on Project…

Select Insert/Module

Page 11: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Launching VBA

Congratulations, you’re ready to begin programming!

Page 12: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Programming Basics

Page 13: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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

Page 14: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Executing a VBA Function

Page 15: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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?

Page 16: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Excel/VBA Tip

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

P=worksheetfunction.pi

D=worksheetfunction.degrees(x)

Page 17: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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”

Page 18: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Review Questions

Page 19: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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

Page 20: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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

Page 21: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

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

Page 22: ME 142 Engineering Computation I Variables, Equations & Functions Introduction to Basic Programming Language.

Homework Help ‘n Hints