Excel VBA Constants in Office 365 ~ Useful Guide!!
Tags: vbamacros, vbaconstants
Published: 2020-12-18 08:50:15

---
title: Excel VBA Constants in Office 365 ~ Useful Guide!!
published: true
date: 2020-12-17 12:14:03 UTC
tags: VBAMacros,VBAconstants
canonical_url: https://geekexcel.com/excel-vba-constants/
---

In Excel, [VBA](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/) Constants is a named memory location that is used to hold the value whose value can’t be changed in the program. In this tutorial, we are going to see how to **declare a constant and assign a value to it in Excel VBA**.
## VBA Constants -Syntax:

- Use the below code to declare a constant in Excel VBA.

**Const <constant\_name> As <Data type> = <Constant\_value>**

## Data types:

In VBA, all variables and constants have a data type. The following table shows the VBA Data Types along with its range of values.

<style> th, td{ padding: 20px; } </style>

| Data 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 | **If no decimal is in use:** +/- 79,228,162,514,264,337,593,543,950,335

**For (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 integer value |
| Variant (text) | Same as variable-length string |

## Example:

The following steps are used to declare and assign a value for Excel VBA Constants.

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

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

- Just copy the below VBA codes and paste it in the code window.

VBA code for Add date in footer using Macros in Excel

Sub myConstantDeclaration()  
Const myStringCon As String = “Welcome”  
Const MyIntegerCon As Integer = 50  
MsgBox “My Text String is: ” & myStringCon & Chr(10) & “My integer vlaue is: ” & MyIntegerCon & Chr(10)  
End Sub

- You need to save the code.

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

- Then get back to the workbook and select the **Macros** option under the **Developer tab.**

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

- It will display the **Macro**   **dialog box.**
- Here, you need to select your **macros name** and hit the **Run button.**

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

- Now, you will get the output as shown in the below image.

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

## Wind-Up:

In this guide, you can get some clarification on how to use **VBA constants in Excel**. You can also learn the data types available in Excel VBA. Please share your **queries** in the comment box. We will assist you. Thank you. To learn more, check out our website **[Geek Excel](https://geekexcel.com/)!! **