Module 1 - Block 4 - Variable Definition and Use
Module 1 - Block 4 - Variable Definition and Use
VBA Programming
Dealing with Variable
Why do we need variables
VBA Programming
Dealing with Variable
Different kind of scope
VBA Programming
Dealing with Variables
Defining a variable
This element of storage is disconnected from the
cells
VBA Programming
Dealing with variables
What kind of variable do we need
VBA Programming
Dealing with variable
or
Private LaVariable as Integer
Dim LaVariable as Integer
(The defined variable is visible only in the module that contains the definition)
VBA Programming
Dealing with Variable
Global and local variable, Scope
This is the content of module 1 This is the content of module 2
Local variables
Public Sub MacroB() Public Sub MacroD()
VBA Programming
Dealing with Variable
Global and local variable, Scope
This is the content of module 1 This is the content of module 2
VBA Programming
Dealing with Variables
Assignment operator
Example
i=10
TVA=19.6
VBA Programming
Dealing with Variables
Arithmetics operators
Arithmetic operators
VBA Programming
Dealing with variables
Implicit or Explicit
It’s not always necessary to define the variable explicitly using a dim
statement. The variable can also be automatically defined when first
used. The datatype depends on the first value stored in the variable.
If you want to make the variable definition mandatory you must add the
statement « Option Explicit » at the beginning of your module
Option explicit
VBA Programming
Dealing with Variable
a macro that uses Variables
Sub MacroWithSomeVariables()
Local Variables definition
End Sub
Using the variables
VBA Programming
Dealing with Variables
or simply
Dim MyVariable
VBA Programming
Dealing with Variables
Checking the kind of information in a Variant
varType(<Variable>)
VBA Programming
Dealing with Variables
return values for VarType
VBA Programming
Object variable
Complex objects
VBA Programming
Object variable
Assigning a value to a complex object
VBA Programming
Object variable
Assigning a value to a complex object
VBA Programming