0% found this document useful (0 votes)
7 views18 pages

Module 1 - Block 4 - Variable Definition and Use

Uploaded by

Didier Doudou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Module 1 - Block 4 - Variable Definition and Use

Uploaded by

Didier Doudou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Dealing with variables

VBA Programming
Dealing with Variable
Why do we need variables

When creating a complex macro that does


calculations we may need to store temporary
result

We may store that information within the Excel


cells (in the worksheet).

But if we don’t want to alter the workbook by


storing temporary result we will be able to define
variables

VBA Programming
Dealing with Variable
Different kind of scope

A variable is an element of storage can be


defined

outside of the macros (directly in the module)


We call this a global variable
or
Inside the macro
We call this a local variable

VBA Programming
Dealing with Variables
Defining a variable
This element of storage is disconnected from the
cells

Each variable is assigned


an identifier
and a
datatype
defining its properties

Dim MyVariable as Integer

VBA Programming
Dealing with variables
What kind of variable do we need

Boolean True False


Byte 0 255
Integer -32768 32767
Long -2147483648 2147483647
Single -3,4E38..-1,4E-45 1,4E-45..3,4E38
Double -1,79E308..-4,94E-324 4,94E-324..1,79E308
Date 1er Janvier 100 31 Décembre 9999
Currency -922 337 203 685 477,5808 922 337 203 685 477,5807

String Allows us to store text information


Variant Any kind of data

VBA Programming
Dealing with variable

A global variable is directly defined in the module


it may be public or private

Public LaVariable as Integer


(the defined variable is visible in all the modules)

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

Dim MyGlobalNumber_M1 As Integer Dim MyGlobalNumber_M2 As Integer

Private PrivateGlobalVariable_M1 As Integer Private PrivateGlobalVariable_M2 As Integer

Public PublicGlobalVariable_M1 As Integer Public PublicGlobalVariable_M2 As Integer

Public Sub MacroA() Public Sub MacroC()

Dim LocalVariableInA As Currency Dim LocalVariableInD As Currency

Global variables in module 1 Global variables in module 2


End Sub End Sub

Local variables
Public Sub MacroB() Public Sub MacroD()

Dim LocalVariableInB As Currency Dim LocalVariableInD As Currency

End Sub End Sub

VBA Programming
Dealing with Variable
Global and local variable, Scope
This is the content of module 1 This is the content of module 2

Dim MyGlobalNumber_M1 As Integer Dim MyGlobalNumber_M2 As Integer

Private PrivateGlobalVariable_M1 As Integer Private PrivateGlobalVariable_M2 As Integer

Public PublicGlobalVariable_M1 As Integer Public PublicGlobalVariable_M2 As Integer

Public Sub MacroA() Public Sub MacroC()


Visible in both module

Dim LocalVariableInA As Currency Dim LocalVariableInD As Currency

Only visible in the module 1 Only visible in the module 2


End Sub End Sub

Public Sub MacroB() Public Sub MacroD()

Dim LocalVariableInB As Currency Dim LocalVariableInD As Currency

End Sub End Sub

VBA Programming
Dealing with Variables
Assignment operator

Assigning a value to a variable

<Name of the variable> = <Value to be stored>

Example

i=10

TVA=19.6

VBA Programming
Dealing with Variables
Arithmetics operators

Arithmetic operators

+ Addition A+10, A+B (15, 10)


* Multiplication A*2, B*A (10, 25)
/ Division A/B, A/2 (floating point result)
- Substraction A-B, B-1 (0, 4)
& Concatenation "V“ & "B" & "A" ("VBA")
Mod Modulo A Mod 2 (1)
\ Euclidian division B \ 4 (integer part of the quotient)
^ Exponentiation 8^2 (64)

Assuming that A and B are both numbers and equal to 5

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.

leResultat = Cells(25,11).value + Cells(25,10).Value

But it’s better programming to explicitly define 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

Dim unEntier as Integer


Dim unTexte as String, unReel as Single
Dim unBooleen as Boolean

VBA Programming
Dealing with Variable
a macro that uses Variables
Sub MacroWithSomeVariables()
Local Variables definition

Dim Number1 As Integer, Number2 As Integer, Val As Integer


‘ Gets the value in cell B1 and put this in Nombre1
Number1 = Cells(1, 2).Value Assignements
‘ Gets the value in cell B2 and put this in Nombre1
Number2 = Cells(2, 2).Value
Assignements
‘ Gets the value in cell B3 and put this in Val
Val = Cells(3, 2).Value
Assignements
Cells(Number1, Number2).Value = Val

End Sub
Using the variables

VBA Programming
Dealing with Variables

To define a variable as a Variant we may use one


of the following

Dim MyVariable as Variant

or simply

Dim MyVariable

VBA Programming
Dealing with Variables
Checking the kind of information in a Variant

A Variant can hold any kind of information, in


order to know at run time which kind of
information a Variant contains we can use

varType(<Variable>)

This function will return a predefined value that


will tell us what kind of information is contained in
the variable at that time

VBA Programming
Dealing with Variables
return values for VarType

Possible return value for the varType Function


Constant Value Description
vbEmpty 0 Empty (Not yet initialized)
vbNull 1 Null (No valid data)
vbInteger 2 Integer
vbLong 3 Long integer
vbSingle 4 Single
vbDouble 5 Double
vbCurrency 6 Currency
vbDate 7 Date
vbString 8 String
vbObject 9 Object
vbError 10 Error
vbBoolean 11 Boolean
Variant (only if we’re facing a
vbVariant 12
table of variant)
vbDataObject 13 Data Access Object
vbDecimal 14 Décimal value
vbByte 17 Byte
vbUserDefinedType 36 User defined datatype
vbArray 8192 Array

VBA Programming
Object variable
Complex objects

We may also need to define variable that can


hold a workbook, a worksheet, a range of cells….

Dim MyBook as WorkBook


Dim MySheet as WorkSheet
Dim LaPlage as Range

VBA Programming
Object variable
Assigning a value to a complex object

In order to assign a value to that kind of variable


we need to use the following syntax

Dim MyBook As Workbook

Set MyBook = Workbooks.Add


MyBook.SaveAs Filename:="Leclasseur.xlsx"
MyBook.Close

VBA Programming
Object variable
Assigning a value to a complex object

Dim MySheet As Worksheet


Set MySheet = ActiveSheet
Sheets(2).Select
Cells(1, 1) = 10
MySheet.Select

VBA Programming

You might also like