# VBA Variables

In VBA, and in programming in general, variable is a resource for saving information.

    Variable = "An information"


Variables are used to store data, such as numbers or texts.

Before using a variable it is advisable to declare it, indicating its name (and its data type, which will be seen later).

A declaration is made through the word Dim, as in the example below:

    Dim VarText
Dim VarNumber

VarText = "I was previously declared"


## Naming a Variable

The variable name, which comes after the Dim keyword, must comply with the following rules:

• It must initiate with an alphabetical character

## Automatic Data Conversion

It is often possible to associate the content of one data type with another when declaring variables. However, VBA will attempt to convert the content to the declared data type (E.g. a number as text). Failure to do so will return an error.

    Dim VarText As String
VarText = 1


The VarText above will be a text (and you will not be able to perform mathematical calculations with it).

    Dim VarNumber As Integer
VarNumber = "Two"


VBA will try to assign "Two" to VarNumber performing the automatic conversion, but it will return an error:

In order for the conversion to work the Assassination should be done with "2" (instead of "Two").

When debugging a code, try to use the Locals Window in which all data types are explicit.

### Display Translation

Boolean variables only accept the values True and False. However, if your system language is not English, a translated equivalent may be displayed in message boxes:

Although the MsgBox displays "Verdadeiro" (which is the Portuguese-BR translation for true), in the code the value remains as True.