Locked lesson.
About this lesson
How to assign strings, values or objects to variables and use them in your code.
Exercise files
Download this lesson’s related exercise files.
Setting variables.xlsm26.1 KB Setting variables - Completed.xlsm
26 KB
Quick reference
Setting variables
Assigning values and objects to variables.
When to use
When you wish to assign something to a variable that you have created.
Instructions
Variable Assignment
How you assign something to a variable depends on the type of data you wish to assign:
- Values are set using the = statement (e.g. VariableName = 5)
- Text variables must be wrapped in quotes (e.g. VariableName = “Text”)
- Object variables are assigned using the Set statement (e.g. Set VariableName = Target [worksheet, range, etc.])
Common Data Types
Type | Use For | Assign Via |
---|---|---|
Boolean | TRUE/FALSE | = |
Integer | Numbers between -32,768 and 32,767 | = |
Long | Really large numbers (no decimals) | = |
Double | Really large numbers (with decimals) | = |
Date | Dates | = |
String | Text | = |
Objects | Object, workbook, worksheet, range, etc... | Set |
Variant | Determined at runtime | Set or = (as req'd by true type |
Lesson notes are only available for subscribers.