220 likes | 404 Views
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.
E N D
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 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) Identify: • Variables - entered by user • Material cost • Labor cost • Constants -set by programmer • Overhead percentage • Profit percentage
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
Start or Stop DataInput/Output Process steps PredefinedProcess Decisions Flow Charts • Schematic Drawing of Process • Use Standard Symbols • Flow from Top to Bottom • Connect with Arrows
Start Decide you need a cup of coffee Get cup Go to coffee pot Is potempty? Makecoffee Yes No Fill cup End Problem: Get Coffee! • Any task can be flow-charted • Each step could have its own chart • Note decision flow
Get: Labor costMaterial cost Determine:Total costBid Price Show Bid Price Quit Cost Model • Start • Get user inputs • Material • Labor • Determine cost • Determine price • Output results • Quit Start
Planning for Programming • Observe the process to be automated • Flowchart • Write Pseudo Code • Edit Code with VBA Editor
Ask user for labor cost [vLabor] and material cost [vMaterial] Get: Labor costMaterial cost [vCost] = [vLabor] + [vMaterial] [vPrice] = [vCost]*(1+[cOH]*[cProfit]) Determine:Total costBid Price Show Bid Price Display message with [vPrice] Quit Pseudo Code Write steps, formulas needed Start Pseudo Code often ends up as comments in the 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 • Data type – what can be stored? • String – text • Integer • Single or Double • Boolean – True/False • Date • Variant (Default) Dim – short for “dimension” • 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 asText • 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 nested vFirstName = 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