Excel VBA Variable Types

Introduction

In this article we'll be discussing the Excel VBA variable types. If you're familiar with any other programming language the same basic variables types will be available in VBA.

The Excel VBA Variable Types

Variables are also known as the storage of different kinds of data types and constants for your worksheets. We suggest using variables more than constant because you can easily change a variable while constants remain fixed. We have two main types of variables; numerical and non-numerical.

Numerical Variables

When we store numbers, a numerical data type is used to store those. See below examples of data types:

  • Long - this is used to hold longer data types like longer values or numbers. It is longer than the usual 32768. But when using long data types, it can hold values from -2,147,483,648 to 2,147,483,648.
  • Single - this is created to store decimal values which are not exceeding two digits on decimals.
  • Integer - Integer can not hold long values, it can only hold values up to 32768 only. Any exceed values will result in an error. An integer does not accept decimal numbers, hence if we encounter one, it will be rounded off to the nearest decimal numbers.

We also have other types of numerical variables which are byte, decimal, currency, date and time.

Non-numerical Variables

See below examples of non-numerical variables:

  • String - this is used in storing text values. This can also hold two types of string values which are the fixed strings and the variable-length strings.
  • Boolean - this is used for storing true or false values.

-Object - this is used to store object data types like workbooks, worksheets, sheets ranges, cells, and more.

-Double - this is used for storing numbers with decimals.

-Date - this is used for storing dates.

-Variant - this is used with both numerical and non-numerical types of data.

Naming Variables

We have to follow some rules when naming a variable:

  • Private, next, loop, are example of reserved keys. And, reserved keys can never be a variable name.
  • Special characters are not allowed to be part of your variable name. Example of special character: !, @, #, $, %, ^, & and more.
  • Underscore is allowed when naming a variable. Spaces are not allowed.
  • You can use a mixed case of letters. VBA is not case-sensitive.
  • The first letter of the variable's name must be a letter. But we can use any of the following afterward: letters/alphabets, numbers, and/or punctuation marks.
  • The name should be concise. The maximum number of characters a variable name can be is 255.

Variables in your code

Let's see an example below:

Sub example1()  

Dim product_Name As String  
product_Name = "Hello!!"  
MsgBox product_Name  

End Sub  

The variable created here was named product_Name and we assigned a string to it with a value of "Hello!!".

Let's take a look a the result after running the code:

Screenshot of a variable used in a code

Conclusion

We have covered the variable types in VBA and the naming conventions involved in creating a variable. Get familiar with how to use variables because they will help you be more productive when writing your scripts in VBA.

Published: 
July 8, 2020
Author:
500 Rockets
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram