Introduction to Variables
- 02:39
Understanding variables in Excel VBA.
Downloads
No associated resources to download.
Transcript
Variables and looping procedure are the building blocks of effective and efficient Excel VBA Coding, the use of variables and looping procedures enables much quicker analysis and automation that isn't possible in the front end of Excel.
The macro recorder will not declare variables nor will it build looping code for us to use.
And as such, we need to build our knowledge of variables and looping procedures from first principles.
Think of a variable as a storage unit in Excel VBA.
An item can be assigned to a variable in VBA code and we can also assign other objects with the value of our variable.
The variable does not have to sit in a cell in the front end of Excel, but we can use a variable from Excel VBA to populate cells in a spreadsheet when using variables in our code, it's good practice to state what type of data we want the variable to hold.
We don't have to do this, but stating the data type reduces the size of the code and can therefore make it run much more quickly.
Common data types are string data types.
A string data type variable can hold any text.
Integer data types.
Integer data types can hold any number between plus 32,000 and minus 32,000.
However, integer data types can only hold whole numbers.
The long data type is designed to hold whole bigger numbers, whereas the double data type can hold big numbers, including decimals.
And finally, the Boolean data type will hold the values of either true or false when naming a variable, be descriptive.
Our objective should be to make the variable name as useful as possible and unlikely to be confused with an existing name used in VBA.
To discern our variable from other names, we can use a mixture of uppercase and lowercase letters.