Excel Office 365 VBA Variables & Data Types – A Complete Guide!!
Tags: vbamacros, vbavariablesinexcel
Published: 2020-12-17 11:30:39

---
title: Excel Office 365 VBA Variables & Data Types – A Complete Guide!!
published: true
date: 2020-12-17 09:50:50 UTC
tags: VBAMacros,VBAVariablesinExcel
canonical_url: https://geekexcel.com/excel-office-365-vba-variables-data-types-a-complete-guide/
---

Variables ( **a named memory location** )are used in all computer programs to store specific values. You can easily get that value from your system and display the result. In this post, we will guide you to know how to use [**VBA**](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/) **Variables** and **Data Types** in **Excel Office 365.**
## VBA Variable Declaration:

- If you want to **declare** a **VBA variable** as an **integer,** use the below code.

**Dim myNum as Integer** or **Dim myNum%**

- The above code declares a variable **“myNum”** as an integer type.

## Initialize a VBA Variable:

- You can initialize a VBA Variable as shown below.

**Dim myNum as Integer**

**myNum = 18**

- You have to declare the variable first, then initialize the value for that variable.

## Date Types:

Generally, data types are used to know the type of data. There are plenty of data types available for declaring the variables. Let’s see them in the given table.

| **Type** | **Range of Values** |
| --- | --- |
| Byte | 0 to 255 |
| Boolean | True or False |
| Integer | -32768 to 32767 |
| Long | -2,147,483,648 to 2,147,483,648 |
| Single | For Negative Values – -3.402823E+38 to -1.401298E-45, For Positive Values – 1.401298E-45 to 3.402823E+38 |
| Double | For Negative values – -1.79769313486232e+308 to -4.94065645841247E-324, For Positive values – 4.94065645841247E-324 to 1.79769313486232e+308 |
| Currency | -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
| Decimal | No decimal places – +/- 79,228,162,514,264,337,593,543,950,335, 28 decimal places – +/- 7.9228162514264337593543950335 |
| String (fixed length) | 1 to 65,400 characters |
| String (variable length) | 0 to 2 billion characters |
| Date | January 1, 100 to December 31, 9999 |
| Object | Any embedded object |
| Variant (numeric) | Any value as large as double |
| Variant (text) | Same as variable-length string |

## Practical Example:

The following examples will show you how to declare and initialize a VBA variable in Excel. Kindly, do as follows.

- First, you need to **open** the **Visual Basic Editor** by clicking the shortcut [**ALT** + **F11**](https://geekexcel.com/shortcut-to-open-vba-editor-in-microsoft-excel-365/) Keys.
- Then, on the **Insert Tab, select** the **Module** option from the menu.

![Insert Tab](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Insert-Tab-20.png)<figcaption id="caption-attachment-19831">Insert Tab</figcaption>

- Then, you need to **enter the below given VBA code** in the code window.

VBA Variables

Sub myVariables()  
Dim myNum As Integer  
myNum = 18  
Dim myStr As String  
myStr = “Geek Excel”  
Dim RedletterDay As Date  
RedletterDay= 4 / 12 / 2020  
MsgBox “myNum is” & myNum & Chr(10) & “my string is ” & myStr & Chr(10) & “RedletterDay of the Year” & RedletterDay  
End Sub

- Finally, you need to **save the code.**

![Enter the Code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Enter-the-Code.png)<figcaption id="caption-attachment-19916">Enter the Code</figcaption>

- Now, you need to **close** this **window.**
- Then, go back to the workbook and **run** the **macro** by **clicking** the **Macros** option under the **Developer Tab.**

![Click Macros option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Click-Macros-option.png)<figcaption id="caption-attachment-19833">Click Macros option</figcaption>

- Then, it will display the **Macro Dialog Box** , where you need to **select** the **macro name** and **hit** the **Run** button.

![Macro Dialog Box](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Macro-Dialog-Box.png)<figcaption id="caption-attachment-19917">Macro Dialog Box</figcaption>

- Now, you will see the output as shown in the below screenshot.

![Output](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/12/Output-20.png)<figcaption id="caption-attachment-19918">Output</figcaption>

## A Quick Summary:

In this post, you will easily understand how to use VBA **Variables** and **Data Types** in **Excel**   **Office 365**. I Hope, the given practical example lets you know how to declare and initialize variables in Excel VBA. Kindly, leave your **feedback** in the comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!