SlideShare a Scribd company logo
1 of 26
Excel Macros Level 1 Variables, Data Types, and Constants
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
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
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
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
Activities In the Unit 5 Activities complete: Activity 1: Auto Fill Activity 2: Programming an Auto Fill 4/29/2010 M. Campbell - 2010 6
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
Variables Dim count As Integer count = 7 4/29/2010 M. Campbell - 2010 8
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
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
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
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
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
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
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
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
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
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
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
Activities In the Unit 5 Activities complete: Activity 3: Variable Scope 4/29/2010 M. Campbell - 2010 20
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
Variable Lifetime Lifetime determines existence of a variable Scope determines visibility of a variable 4/29/2010 M. Campbell - 2010 22
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
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
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
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

More Related Content

What's hot

2 programming with c# i
2 programming with c# i2 programming with c# i
2 programming with c# isiragezeynu
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C ProgrammingKamal Acharya
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in csmumbahelp
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variablesyarkhosh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Dushmanta Nath
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming ConventionsMicheal Ogundero
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in CArpana shree
 
Mca1020 programming in c
Mca1020  programming in cMca1020  programming in c
Mca1020 programming in csmumbahelp
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cciFahim Khan
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NETJaya Kumari
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
User define data type In Visual Basic
User define data type In Visual Basic User define data type In Visual Basic
User define data type In Visual Basic Shubham Dwivedi
 
Check my answer to pass data from one webform to the next, you ca
 Check my answer to pass data from one webform to the next, you ca Check my answer to pass data from one webform to the next, you ca
Check my answer to pass data from one webform to the next, you calicservernoida
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
 

What's hot (19)

2 programming with c# i
2 programming with c# i2 programming with c# i
2 programming with c# i
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in c
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Data type in c
Data type in cData type in c
Data type in c
 
Mca1020 programming in c
Mca1020  programming in cMca1020  programming in c
Mca1020 programming in c
 
C++
C++C++
C++
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cci
 
Shanks
ShanksShanks
Shanks
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NET
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
User define data type In Visual Basic
User define data type In Visual Basic User define data type In Visual Basic
User define data type In Visual Basic
 
Check my answer to pass data from one webform to the next, you ca
 Check my answer to pass data from one webform to the next, you ca Check my answer to pass data from one webform to the next, you ca
Check my answer to pass data from one webform to the next, you ca
 
C++ programing lanuage
C++ programing lanuageC++ programing lanuage
C++ programing lanuage
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 

Viewers also liked

Viewers also liked (8)

Real World Retrospectives
Real World RetrospectivesReal World Retrospectives
Real World Retrospectives
 
Chapter 3 Excel Macros
Chapter 3 Excel MacrosChapter 3 Excel Macros
Chapter 3 Excel Macros
 
Chapter 2: Preliminaries
Chapter 2: PreliminariesChapter 2: Preliminaries
Chapter 2: Preliminaries
 
Unit 6: Functions and Subroutines
Unit 6: Functions and SubroutinesUnit 6: Functions and Subroutines
Unit 6: Functions and Subroutines
 
Unit 8: Control Statements
Unit 8: Control StatementsUnit 8: Control Statements
Unit 8: Control Statements
 
Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2
 
Unit 7: Built-In Functions
Unit 7: Built-In FunctionsUnit 7: Built-In Functions
Unit 7: Built-In Functions
 
Opportunities - English version
Opportunities - English versionOpportunities - English version
Opportunities - English version
 

Similar to Unit 5: Variables (20)

C language by Dr. Gholkar D. R.
C language by Dr. Gholkar D. R.C language by Dr. Gholkar D. R.
C language by Dr. Gholkar D. R.
 
Introduction to F# 3.0
Introduction to F# 3.0Introduction to F# 3.0
Introduction to F# 3.0
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Intake 38 2
Intake 38 2Intake 38 2
Intake 38 2
 
c programming Pankaj Panjwani.pptx
c programming Pankaj Panjwani.pptxc programming Pankaj Panjwani.pptx
c programming Pankaj Panjwani.pptx
 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application
 
c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
 
Bca5020 visual programming
Bca5020  visual programmingBca5020  visual programming
Bca5020 visual programming
 
Bca5020 visual programming
Bca5020  visual programmingBca5020  visual programming
Bca5020 visual programming
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 

Unit 5: Variables

  • 1. Excel Macros Level 1 Variables, Data Types, and Constants
  • 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
  • 8. Variables Dim count As Integer count = 7 4/29/2010 M. Campbell - 2010 8
  • 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