Unit 5: Variables


Published on

Notes for Unit 5: Variables, Data Types, and Constants.

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit 5: Variables

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