How to Define Variable in Excel VBA (2 type variable)

Welcome to Excel Avon

What is Variable in Excel

Variables and data types are used in all programs to store and represent data in the computer. Variables are specific values that are stored in computer memory or in storage system. Later, can be use in code and execute. In this article, we will learn about VBA variables. Variables do not store fixed values. When we define a variable, we always store the value in different range. A small byte of variable memory can also store a large amount of data. All variables have their own memory size. All variables have their own storage limit in which they cannot store more than their limit, that’s why if you will be asked to store integer byte in one byte, then it will be able to store values ​​from 0 to 255. If the value is stored in 256 then you have to use long or integer whose limit can store values ​​from -32768 to 32767, similarly there are many variables. There are two types of data types in VBA.

There are several VBA data types, which can be divided into two main categories, numeric and non-numeric data types.

How to define Types of Variables

DOWNLOAD USED EXCEL FILE FROM HERE>>

First of all, we will go to developer tab then click on macros.

define-variable2

and the insert will then go into the module insert.

define-variable3

Numeric Data Type

Use of numeric data types where we need to store numbers. Numbers can be of any type such as integer, decimal number, currency, date and time.

In the image below you can see all the numeric data types. If we try to use decimal values in these four data types of Bytes, Integers, Longs, it will round them again to the nearest possible whole number.

Decimal values can be stored in a single data type, but it can only hold two-digit decimal values.

define-variable

Now we will define the variable. 

 Let’s a As Bytes, b As Integers, c As Long, d As Single, e As Double and f As Currency, now you can see that we have considered a as a byte and if we want to print a = 123 it will be done because the range of byte is 0 to 255 and 123 will be between 0 to 255.

define-variable4

Similarly, we can do in integers also the range in integers is from -32767 to 32767, so take it as b = 10012

 define-variable5

Now let us consider single double and currency variable

define-variable4.

Will write debug print and click run button

define-variable5 (2)

And you will see in the immediate window that you are printing f, currency. The currency data type stores values greater than 4 digits of decimal. as you can see in the image.

define-variable5 (1)

Now we will print the double variable The double data type prints the store value greater than 2 digits of the decimal. debug. print e and press run button.

Non-Numerical Data Type

But it takes those values which are not in numeric data types, they come in non-numeric like Boolean, text, letter. 

String is used for text. It has 2 types, fixed and variable-length.

Boolean is used for a logical data type. When we need to get the answer as TRUE or FALSE.

Date is used in date type values.

Object variables like Worksheet, Workbook, Sheets, Range which are in Excel come under it.

Variants are used for both numeric and non-numeric values.

define-variable1

In sheet we can use Non_Numeric Variable ()

define-variable51

Let A as String , B as Boolean , C as String , D as Object and E as Variant Now you can see that we have treated a byte as

define-variable54

and if we write a = any text then it will print but before and after text we will use inverted comma (“).

Boolean: This logical data type is used when we need to get the answer as TRUE or FALSE.

When we use date variable then we will use (#) before and after date.

When we use the object, we will write the set and then we will write our variable data as you can see. Object variables such as Worksheets, Workbooks, Sheets, Range which are there in Excel comes under it.

define-variable55

Variant is used for both numeric and non-numeric values. This means that you can use any variable in it.

DOWNLOAD USED EXCEL FILE FROM HERE>>

Learn More Topic in VBA  here

So, I hope you have understood How to Define Variable in Excel VBA and for more information, you can follow us on Twitter, Instagram, LinkedIn, and YouTube as well.

You can also see well-explained video here

Leave a Reply