variables, equations & functions introduction to basic programming...

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

Upload: doankien

Post on 22-Apr-2018

216 views

Category:

Documents


1 download

TRANSCRIPT

ME 142Engineering Computation I

Variables, Equations & Functions

Introduction to Basic Programming

Language

Enabling the Developer’s Ribbon

Launching VBA

Enable Developer’s Tab

Once enabled, should remain enabled

Select Visual Basic from Developer’s Tab

Shortcut key: Alt+F11

Launching VBA

Right click on

Project…

Select

Insert/Module

Launching VBA

Congratulations, you’re ready to begin programming!

Tips for Beginning VBA

Begin by downloading the proper template

file

Make sure you are working in Module 1

Place your code in this module

Don’tforget to enter your ProbID

ProbID = “12AB”

Example Program

Develop a function to calculate and return

the cube root of a number.

Use template for PP 4.2.4

Use ProbID = “12AB”

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 Problem

Develop 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