programming with vba

22
Programming with VBA Planning for Programming Variables and Constants Assignment Statements Functions Programming

Upload: aladdin-delaney

Post on 03-Jan-2016

63 views

Category:

Documents


4 download

DESCRIPTION

Programming with VBA. Planning for Programming Variables and Constants Assignment Statements Functions Programming. Planning for Programming. Observe the process to be automated. Price = Cost + OH + Profit. Observe the Process. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming with VBA

Programming with VBA Planning for Programming Variables and Constants Assignment Statements Functions Programming

Page 2: Programming with VBA

Planning for Programming

Observe the process to be automated

Page 3: Programming with VBA

Observe the Process

Price = Cost + OH + Profit

Identify: Variables - entered by user

Material cost Labor cost

Constants -set by programmer Overhead percentage Profit percentage

State the problem:Given the cost of materials and labor, and knowing the mark-up for overhead and profit, determine the price to bid.

Identify the formula to use: Price = (Materials + Labor) * (1 + Overhead + Profit)

Page 4: Programming with VBA

Observe the Process Having identified:

Formula to Use Variables Constants

Define specifically: Inputs – items the user will enter Outputs – form and media

Page 5: Programming with VBA

Planning for Programming

Observe the process to be automated

Flowchart

Page 6: Programming with VBA

Flow Charts Schematic Drawing

of Process Use Standard

Symbols

Flow from Top to Bottom

Connect with Arrows Decisions

Start or Stop

Process steps

DataInput/Output

PredefinedProcess

Page 7: Programming with VBA

Problem: Get Coffee! Any task can be

flow-charted Each step could

have its own chart

Note decision flow

Start

Decide you need

a cup of coffee

Get cup

Go to coffee pot

Is potempty?

Fill cup

No

End

Yes Makecoffee

Page 8: Programming with VBA

Cost Model Start Get user inputs

Material Labor

Determine cost Determine price Output results Quit

Start

Get: Labor cost

Material cost

Determine:Total costBid Price

Show Bid Price

Quit

Page 9: Programming with VBA

Planning for Programming

Observe the process to be automated

Flowchart

Write Pseudo Code Edit Code with

VBA Editor

Page 10: Programming with VBA

Pseudo Code

Display message with [vPrice]

[vCost] = [vLabor] + [vMaterial] [vPrice] = [vCost]*(1+[cOH]*[cProfit])

Ask user for labor cost [vLabor] and material cost [vMaterial]

Write steps, formulas needed

Pseudo Code often ends up as comments in the VBA code

Start

Get: Labor cost

Material cost

Determine:Total costBid Price

Show Bid Price

Quit

Page 11: Programming with VBA

Write VBA Code

Page 12: Programming with VBA

Variables and Constants Storage “bins” in memory Identified by a unique name Variables can be changed by the user

vName = InputBox(“Enter your name”) Constants can only be changed by

editing the VBA code Const cMarkUp = 0.25

Page 13: Programming with VBA

Declaration Statements

Choose a bin and label it!Dim myVar as String

Dim – short for “dimension”Data type – what can be stored? String – text Integer Single or Double Boolean – True/False Date Variant (Default)

Variable names Must be unique Can’t be same as Sub Can’t use key words Mixed case traps errors

Page 14: Programming with VBA

Declaration and Scope Procedure Level – Dim

Dim vName as Text Place below Sub() Value resets to Empty after End Sub

Module Level Place ahead of all Sub() statements Value from one Sub() available in

others All procedures in all Modules – Public

Public vProfit as Single

Page 15: Programming with VBA

Constants Declarations

Const myDogs as Integer = 4 Must appear before assignment

statements Built-in

Typically named “vb????” vbYes vbRed

Look at Help under Constants

Page 16: Programming with VBA

Arrays Array declarations

Dim MyArray(1 to 100) as Integer Dim MyBigArray(1 to 5, 1 to 10) as

String Referencing an array

MyBigArray(3,5) = “Bob” MyArray(2) = “Mary” Error!

Redim

Page 17: Programming with VBA

Assignment Statements Left side defines object, property, or

variable to be changed Right side defines desired result

vName = InputBox(“Enter your name”) Change occurs after line is processed Concatenation, Replacement

vName = “Name: ” & vName

Page 18: Programming with VBA

VBA String Functions

If vText = “Hi, Bob” Len(vText) = 7 Left(vText, 5) = “Hi, B” Right(vText, 2) = “ob” Mid(string, start, [length])

Mid(vText, 3, 4) = “, Bo” InStr(start, string1, string2)

InStr(1, vText, “,”) = 3

Page 19: Programming with VBA

VBA String Functions

Given vName = “Williams, Ron”How would you extract “Ron”? Find the position of the comma

vCommAt = InStr(1, vName, “,”)

Use the Mid() function starting 2 characters after the comma

vFirstName = Mid(vName, vCommAt+2)

Functions can be nestedvFirstName = Mid(vName, Instr(1, vName, “,”)+2)

Page 20: Programming with VBA

VBA Type Conversions Format(Number, “Format”)

Format(123.4, “$###.00”) = “$123.40”

Val(String) Val(“125 lbs”) = 125

DateValue(String) DateValue(“January 30, 2003”) =

37,624

Page 21: Programming with VBA

Methods Actions to be taken

Select Copy Paste Clear Delete

Connect to object with period Range(“B3”).Clear

Page 22: Programming with VBA

Try it out!