Locked lesson.
About this lesson
Setting up variable dimensions and ensuring the code does so in the correct location.
Exercise files
Download this lesson’s related exercise files.
Creating variables.xlsm25.7 KB Creating variables - Completed.xlsm
26.1 KB
Quick reference
Creating variables
Giving a variable its dimensions.
When to use
When you need to set up a new variable.
Instructions
Defining Dimensions
Variables are "Dimensioned" (Created) using the following statement:
Dim <Name> As <Type>
Naming Tips
- Each variable must have a unique name.
- Names should be short but descriptive
- Names cannot start with a number
- Names cannot contain any spaces or special characters (except underscores)
- The optional “Hungarian Notation” practice prefaces a name with a shorthand data type (e.g. “ws” for worksheet, “i” for integer).
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 |
Where to Create a Variable
- A variable is defined in the code above where it is needed, usually at the top of the routine.
Lesson notes are only available for subscribers.