Excel Macros Level 1Variables, Data Types, and Constants
CommentsText that follows apostraphe ( ' ) is a commentCan comment/uncomment highlighted code using CommentBlock and UncommentBlock buttonsFound on Edit toolbar4/29/2010M. Campbell - 20102p. 47
Line Continuation CharacterActiveSheet.Range("A1").Font.Bold = _TrueTreated as one line by ExcelThe underscore ( _ ) allows you to break a line of code across multiple screen linesCannot break in the middle of a string literal:ActiveSheet.Range("A _1").Font.Bold = True4/29/2010M. Campbell - 20103p. 47This is invalid and will result in an error
ConstantsLiteral Constant: A specific value, date, or text stringe.g. "Donna Smith", #14/09/2010#Symbolic Constant:A name for a literal constantConst Name = "Donna Smith"4/29/2010M. Campbell - 20104p. 48
EnumsShort for Enumerator or EnumerationA structure that holds a set of name valuesActs as a series of constants4/29/2010M. Campbell - 20105p. 49
ActivitiesIn the Unit 5 Activities complete:Activity 1: Auto FillActivity 2: Programming an Auto Fill4/29/2010M. Campbell - 20106
Variables and Data TypesVariable:Memory location that can hold value of a certain data typeData TypeThe kind of data that can be stored in the variableTypes permitted in Excel seen in Table 5-1 on p. 514/29/2010M. Campbell - 20107p. 51
VariablesDim count As Integercount = 74/29/2010M. Campbell - 20108
Programming ConventionsShould always declare variables at beginning of codeAlways give variables an appropriate typeTry to avoid using Variant type It is wasteful and can lead to logic errors4/29/2010M. Campbell - 20109
Option ExplicitAdding Option Explicit to top of Module forces Excel to require all variables have a typeCan also set:Tools -> OptionsCheck Require Variable Declaration 4/29/2010M. Campbell - 201010p. 54
String Data TypeA 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 marksDim name As Stringname = "Mark"4/29/2010M. Campbell - 201011p. 55
String Data TypeTwo types:Fixed-lengthDim name As String * 10name = "Mark"Variable-lengthDim name As Stringname = "Mark"4/29/2010M. Campbell - 201012p. 55Declares a string to hold 10 characters
ArraysA collection of variables that use the same name but are at different index locationsDim cell(1 To 5) As RangeDeclares array of five variables4/29/2010M. Campbell - 201013p. 58
Arrayscell(1) = 9cell(3) = 2Sets those cells to the supplied valueThe number in brackets is the index location4/29/2010M. Campbell - 201014p. 58
Dynamic ArraysCan create array of unspecified size:		Dim cell() As RangeTo (re)size the array:ReDim cell(1 to 10)Resizing an array destroys its contentsTo resize and preserve contents:ReDim Preserve cell(1 to 10)4/29/2010M. Campbell - 201015p. 59
Variable Naming ConventionsOne method is Hungarian notation:Variable name starts with its typee.g. i or int for an integer typeThen a descriptive namee.g. for a integer to hold the loan principal: intPrincipalMore type codes on page 614/29/2010M. Campbell - 201016p. 60
Variable ScopeIndicates the useable region of a variableThree types of scope:Procedure-level (local)Module-level privateModule-level public4/29/2010M. Campbell - 201017p. 62
Variable Scope4/29/201018Public PublicVar As IntegerPrivate PrivateVar As IntegerPublicVar and PrivateVar accessible to both ProcedureA and ProcedureBSub ProcedureA()Dim LocalVarA As IntegerLocalVarA only exists inside of ProcedureASub ProcedureB()Dim LocalVarB As IntegerProcedureB cannot use LocalVarALocalVarB only exists inside of ProcedureBM. Campbell - 2010
Variable ScopeModule-level privateAvailable to all Procedures in this ModuleModule-level publicAvailable to all Procedures in all Modules4/29/2010M. Campbell - 201019p. 62
ActivitiesIn the Unit 5 Activities complete:Activity 3: Variable Scope4/29/2010M. Campbell - 201020
Variable LifetimeDefined as how long a variable retains a value forLocal Variables exist from when Procedure is entered to when it is finishedVariable may be out of scope but still valid (i.e. alive)4/29/2010M. Campbell - 201021p. 64
Variable LifetimeLifetime determines existence of a variableScope determines visibility of a variable4/29/2010M. Campbell - 201022
Static VariablesContinue to hold value after Procedure has quitStatic Variables have:Scope of local variableLifetime of a module-level variable4/29/2010M. Campbell - 201023p. 65
Variable InitializationShould provide an initial value to all variablesDim x As Integer' Declares an integer variable xx = 0			' Initializes x to be 04/29/2010M. Campbell - 201024p. 67
VBA OperatorsListed on page 68Note for Integer division:4/29/2010M. Campbell - 201025p. 684 is the quotient13 \ 3 = 41 is the remainder13 Mod 3 = 1
ConcatenationThe process of appending two strings togetherDim result As Stringresult = "Hello" & " world!"Debug.Print (result)Hello world!4/29/2010M. Campbell - 201026p. 68

Unit 5: Variables

  • 1.
    Excel Macros Level1Variables, Data Types, and Constants
  • 2.
    CommentsText that followsapostraphe ( ' ) is a commentCan comment/uncomment highlighted code using CommentBlock and UncommentBlock buttonsFound on Edit toolbar4/29/2010M. Campbell - 20102p. 47
  • 3.
    Line Continuation CharacterActiveSheet.Range("A1").Font.Bold= _TrueTreated as one line by ExcelThe underscore ( _ ) allows you to break a line of code across multiple screen linesCannot break in the middle of a string literal:ActiveSheet.Range("A _1").Font.Bold = True4/29/2010M. Campbell - 20103p. 47This is invalid and will result in an error
  • 4.
    ConstantsLiteral Constant: Aspecific value, date, or text stringe.g. "Donna Smith", #14/09/2010#Symbolic Constant:A name for a literal constantConst Name = "Donna Smith"4/29/2010M. Campbell - 20104p. 48
  • 5.
    EnumsShort for Enumeratoror EnumerationA structure that holds a set of name valuesActs as a series of constants4/29/2010M. Campbell - 20105p. 49
  • 6.
    ActivitiesIn the Unit5 Activities complete:Activity 1: Auto FillActivity 2: Programming an Auto Fill4/29/2010M. Campbell - 20106
  • 7.
    Variables and DataTypesVariable:Memory location that can hold value of a certain data typeData TypeThe kind of data that can be stored in the variableTypes permitted in Excel seen in Table 5-1 on p. 514/29/2010M. Campbell - 20107p. 51
  • 8.
    VariablesDim count AsIntegercount = 74/29/2010M. Campbell - 20108
  • 9.
    Programming ConventionsShould alwaysdeclare variables at beginning of codeAlways give variables an appropriate typeTry to avoid using Variant type It is wasteful and can lead to logic errors4/29/2010M. Campbell - 20109
  • 10.
    Option ExplicitAdding OptionExplicit to top of Module forces Excel to require all variables have a typeCan also set:Tools -> OptionsCheck Require Variable Declaration 4/29/2010M. Campbell - 201010p. 54
  • 11.
    String Data TypeAstring 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 marksDim name As Stringname = "Mark"4/29/2010M. Campbell - 201011p. 55
  • 12.
    String Data TypeTwotypes:Fixed-lengthDim name As String * 10name = "Mark"Variable-lengthDim name As Stringname = "Mark"4/29/2010M. Campbell - 201012p. 55Declares a string to hold 10 characters
  • 13.
    ArraysA collection ofvariables that use the same name but are at different index locationsDim cell(1 To 5) As RangeDeclares array of five variables4/29/2010M. Campbell - 201013p. 58
  • 14.
    Arrayscell(1) = 9cell(3)= 2Sets those cells to the supplied valueThe number in brackets is the index location4/29/2010M. Campbell - 201014p. 58
  • 15.
    Dynamic ArraysCan createarray of unspecified size: Dim cell() As RangeTo (re)size the array:ReDim cell(1 to 10)Resizing an array destroys its contentsTo resize and preserve contents:ReDim Preserve cell(1 to 10)4/29/2010M. Campbell - 201015p. 59
  • 16.
    Variable Naming ConventionsOnemethod is Hungarian notation:Variable name starts with its typee.g. i or int for an integer typeThen a descriptive namee.g. for a integer to hold the loan principal: intPrincipalMore type codes on page 614/29/2010M. Campbell - 201016p. 60
  • 17.
    Variable ScopeIndicates theuseable region of a variableThree types of scope:Procedure-level (local)Module-level privateModule-level public4/29/2010M. Campbell - 201017p. 62
  • 18.
    Variable Scope4/29/201018Public PublicVarAs IntegerPrivate PrivateVar As IntegerPublicVar and PrivateVar accessible to both ProcedureA and ProcedureBSub ProcedureA()Dim LocalVarA As IntegerLocalVarA only exists inside of ProcedureASub ProcedureB()Dim LocalVarB As IntegerProcedureB cannot use LocalVarALocalVarB only exists inside of ProcedureBM. Campbell - 2010
  • 19.
    Variable ScopeModule-level privateAvailableto all Procedures in this ModuleModule-level publicAvailable to all Procedures in all Modules4/29/2010M. Campbell - 201019p. 62
  • 20.
    ActivitiesIn the Unit5 Activities complete:Activity 3: Variable Scope4/29/2010M. Campbell - 201020
  • 21.
    Variable LifetimeDefined ashow long a variable retains a value forLocal Variables exist from when Procedure is entered to when it is finishedVariable may be out of scope but still valid (i.e. alive)4/29/2010M. Campbell - 201021p. 64
  • 22.
    Variable LifetimeLifetime determinesexistence of a variableScope determines visibility of a variable4/29/2010M. Campbell - 201022
  • 23.
    Static VariablesContinue tohold value after Procedure has quitStatic Variables have:Scope of local variableLifetime of a module-level variable4/29/2010M. Campbell - 201023p. 65
  • 24.
    Variable InitializationShould providean initial value to all variablesDim x As Integer' Declares an integer variable xx = 0 ' Initializes x to be 04/29/2010M. Campbell - 201024p. 67
  • 25.
    VBA OperatorsListed onpage 68Note for Integer division:4/29/2010M. Campbell - 201025p. 684 is the quotient13 \ 3 = 41 is the remainder13 Mod 3 = 1
  • 26.
    ConcatenationThe process ofappending two strings togetherDim result As Stringresult = "Hello" & " world!"Debug.Print (result)Hello world!4/29/2010M. Campbell - 201026p. 68