Variable in VBA

Variables are used to store temporary information, used for execution within the Procedure, Module or Workbook

Variable Naming Rules

1) A Variable name must start with a letter or alphabets. Numbers can be included within the name.

2) A Variable name should not be predefined or keyword

3) All Variables must consist of one continuous string of characters only.It should not contain any embedded space.

4) Variable name should be meaningful or be a noun.


To declare a variable we use the word Dim followed by variable name then the word As followed by the data type.


Dim num As Integer

Dim: Declares the variable

num: The name chosen for this variable

As: Declares the variable’s type

Integer: Variable type or Data Type

And then a value is given to the variable

num = 10

Variable Type Required Memory Description
Integer 2 Bytes Stored whole numbers upto 2 bytes
Long Integer 4 Bytes Stored whole numbers upto 4 bytes
Currency 8 bytes for fixed-point calculations
Single 4 Bytes stores single-precision floating-point
Double 8 Bytes holds double-precision floating-point
Byte 1 Byte Range 0 to 255
String #NA Hold sequence of contiguous characters
Object 4 Bytes Represents any Object reference.
Date 8 Bytes Store dates and times as a real number.
Boolean 2 Bytes Only two possible values, True (-1) or False (0).
Variant 16 Bytes Contain numeric, string, or date. By Default data type is Variant if not explicitly declared



You can declare several variables in one statement.

In the following statement, the variables x , y , and  z are declared as type Integer.

Dim x, y, z as Integer

You can use the Public statement to declare public module-level variables. Public variables can be used in any procedures in the project

Public Name As String


Comments in VBA

Comments are used to document the program logic and the user information. Comments are ignored by the interpreter while execution.

Comments in VBA are denoted by Single Quote ‘ and REM Keyword


Sub Test()

‘Here I have to save my secret code to a variable called sCode

dim sCode as Integer

sCode =1309

REM I can use comment line using REM Keyword also

end Sub

author image

About admin

You Might Also Like...

Online Video Tutorials-Hirdesh Bhardwaj
Team - National Skills Foundation of India
VBA Hirdesh Bhardwaj, Hirdesh Bhardwaj Book

Enjoy the tutorial? Please spread the word :)

Follow by Email