Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | aladdin-delaney |
View: | 63 times |
Download: | 4 times |
Programming with VBA Planning for Programming Variables and Constants Assignment Statements Functions Programming
Planning for Programming
Observe the process to be automated
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)
Observe the Process Having identified:
Formula to Use Variables Constants
Define specifically: Inputs – items the user will enter Outputs – form and media
Planning for Programming
Observe the process to be automated
Flowchart
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
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
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
Planning for Programming
Observe the process to be automated
Flowchart
Write Pseudo Code Edit Code with
VBA Editor
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
Write VBA Code
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
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
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
Constants Declarations
Const myDogs as Integer = 4 Must appear before assignment
statements Built-in
Typically named “vb????” vbYes vbRed
Look at Help under Constants
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
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
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
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)
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
Methods Actions to be taken
Select Copy Paste Clear Delete
Connect to object with period Range(“B3”).Clear
Try it out!