2. Comments Text that follows apostraphe ( ' ) is a comment Can comment/uncomment highlighted code using CommentBlock and UncommentBlock buttons Found on Edit toolbar 4/29/2010 M. Campbell - 2010 2 p. 47
3. Line Continuation Character ActiveSheet.Range("A1").Font.Bold = _ True Treated as one line by Excel The underscore ( _ ) allows you to break a line of code across multiple screen lines Cannot break in the middle of a string literal: ActiveSheet.Range("A _ 1").Font.Bold = True 4/29/2010 M. Campbell - 2010 3 p. 47 This is invalid and will result in an error
4. Constants Literal Constant: A specific value, date, or text string e.g. "Donna Smith", #14/09/2010# Symbolic Constant: A name for a literal constant Const Name = "Donna Smith" 4/29/2010 M. Campbell - 2010 4 p. 48
5. Enums Short for Enumerator or Enumeration A structure that holds a set of name values Acts as a series of constants 4/29/2010 M. Campbell - 2010 5 p. 49
6. Activities In the Unit 5 Activities complete: Activity 1: Auto Fill Activity 2: Programming an Auto Fill 4/29/2010 M. Campbell - 2010 6
7. Variables and Data Types Variable: Memory location that can hold value of a certain data type Data Type The kind of data that can be stored in the variable Types permitted in Excel seen in Table 5-1 on p. 51 4/29/2010 M. Campbell - 2010 7 p. 51
9. Programming Conventions Should always declare variables at beginning of code Always give variables an appropriate type Try to avoid using Variant type It is wasteful and can lead to logic errors 4/29/2010 M. Campbell - 2010 9
10. Option Explicit Adding Option Explicit to top of Module forces Excel to require all variables have a type Can also set: Tools -> Options Check Require Variable Declaration 4/29/2010 M. Campbell - 2010 10 p. 54
11. String Data Type A string is a sequence of characters which can include: Text characters (letters, digits, punctuation) Control characters (vbCrLf -> carriage return) Strings are enclosed in by set of double quotation marks Dim name As String name = "Mark" 4/29/2010 M. Campbell - 2010 11 p. 55
12. String Data Type Two types: Fixed-length Dim name As String * 10 name = "Mark" Variable-length Dim name As String name = "Mark" 4/29/2010 M. Campbell - 2010 12 p. 55 Declares a string to hold 10 characters
13. Arrays A collection of variables that use the same name but are at different index locations Dim cell(1 To 5) As Range Declares array of five variables 4/29/2010 M. Campbell - 2010 13 p. 58
14. Arrays cell(1) = 9 cell(3) = 2 Sets those cells to the supplied value The number in brackets is the index location 4/29/2010 M. Campbell - 2010 14 p. 58
15. Dynamic Arrays Can create array of unspecified size: Dim cell() As Range To (re)size the array: ReDim cell(1 to 10) Resizing an array destroys its contents To resize and preserve contents: ReDim Preserve cell(1 to 10) 4/29/2010 M. Campbell - 2010 15 p. 59
16. Variable Naming Conventions One method is Hungarian notation: Variable name starts with its type e.g. i or int for an integer type Then a descriptive name e.g. for a integer to hold the loan principal: intPrincipal More type codes on page 61 4/29/2010 M. Campbell - 2010 16 p. 60
17. Variable Scope Indicates the useable region of a variable Three types of scope: Procedure-level (local) Module-level private Module-level public 4/29/2010 M. Campbell - 2010 17 p. 62
18. Variable Scope 4/29/2010 18 Public PublicVar As Integer Private PrivateVar As Integer PublicVar and PrivateVar accessible to both ProcedureA and ProcedureB Sub ProcedureA() Dim LocalVarA As Integer LocalVarA only exists inside of ProcedureA Sub ProcedureB() Dim LocalVarB As Integer ProcedureB cannot use LocalVarA LocalVarB only exists inside of ProcedureB M. Campbell - 2010
19. Variable Scope Module-level private Available to all Procedures in this Module Module-level public Available to all Procedures in all Modules 4/29/2010 M. Campbell - 2010 19 p. 62
20. Activities In the Unit 5 Activities complete: Activity 3: Variable Scope 4/29/2010 M. Campbell - 2010 20
21. Variable Lifetime Defined as how long a variable retains a value for Local Variables exist from when Procedure is entered to when it is finished Variable may be out of scope but still valid (i.e. alive) 4/29/2010 M. Campbell - 2010 21 p. 64
22. Variable Lifetime Lifetime determines existence of a variable Scope determines visibility of a variable 4/29/2010 M. Campbell - 2010 22
23. Static Variables Continue to hold value after Procedure has quit Static Variables have: Scope of local variable Lifetime of a module-level variable 4/29/2010 M. Campbell - 2010 23 p. 65
24. Variable Initialization Should provide an initial value to all variables Dim x As Integer' Declares an integer variable x x = 0 ' Initializes x to be 0 4/29/2010 M. Campbell - 2010 24 p. 67
25. VBA Operators Listed on page 68 Note for Integer division: 4/29/2010 M. Campbell - 2010 25 p. 68 4 is the quotient 13 3 = 4 1 is the remainder 13 Mod 3 = 1
26. Concatenation The process of appending two strings together Dim result As String result = "Hello" & " world!" Debug.Print (result) Hello world! 4/29/2010 M. Campbell - 2010 26 p. 68