VBA Variables, Data Types & Constants in Excel

โšก Smart Summary

VBA Variables, Data Types, and Constants in Excel store and label the values a program works with. This page explains variable naming rules, implicit and explicit declaration with Dim, the numeric and non-numeric data types, and how to declare a constant with Const.

  • ๐Ÿ“ฆ Variable: A named value held in memory that the code stores and later reads back.
  • ๐Ÿ”ค Naming Rules: A name is under 255 characters, has no spaces, does not start with a number, and contains no period.
  • โœ๏ธ Declaration: The Dim keyword declares a variable explicitly, for example Dim Num As Integer.
  • ๐Ÿ”ข Numeric Types: Byte, Integer, Long, Single, Double, Currency, and Decimal differ in storage and range.
  • ๐Ÿ”  Non-numeric Types: String, Boolean, Date, Object, and Variant hold text, true or false, dates, and objects.
  • ๐Ÿ”’ Constant: The Const keyword declares a value that cannot be changed, such as Const DaysInYear = 365.

VBA Variables, Data Types and Constants in Excel

Variables are used in almost all computer program and VBA is no different. It’s a good practice to declare a variable at the beginning of the procedure. It is not necessary, but it helps to identify the nature of the content (text, data, numbers, etc.)

VBA Variables

Variables are specific values that are stored in a computer memory or storage system. Later, you can use that value in code and execute. The computer will fetch that value from the system and show in the output. Each of the Excel VBA variable types must be given a name.

To name the variable in VBA, you need to follow the following rules.

  • It must be less than 255 characters
  • No spacing is allowed
  • It must not begin with a number
  • Period is not permitted

Here are some example for Valid and Invalid names for variables in VBA.

VBA Variables Valid Names VBA Variables Invalid Names
My_Watch My.Watch
NewCar1 1_NewCar (not begin with number)
EmployeeID Employee ID ( Space not allowed)

In VBA, we need to declare the variables before using them by assigning names and data type.

In VBA, Variables are either declared Implicitly or Explicitly.

  • Implicitly: Below is an example of a variable declared Implicitly.
  • label=guru99
  • volume=4
  • Explicitly: Below is an example of variable declared Explicitly. You can use “Dim” keyword in syntax
  • Dim Num As Integer
  • Dim password As String

VBA variable is no different than other programming languages. To declare a variable in VBA you use the keyword “Dim.”

Syntax for VBA Variable,

To declare a variable in VBA, type Dim followed by a name:

Sub Exercise ()
        Dim <name>
End Sub

Before we execute the variables we have to record a macro in Excel. To record a macro do the following –

Step 1): Record the Macro 1

Step 2) : Stop Macro 1

Step 3): Open the Macro editor, enter the code for variable in the Macro1

Step 4): Execute the code for Macro 1

Example, for VBA Variable

Sub Macro1()
	Dim Num As Integer
	Num = 99
	MsgBox " Guru " & Num
End Sub

When you run this code, you will get the following output in your sheet.

VBA Variables

Excel VBA Data Types

Computer cannot differentiate between the numbers (1,2,3..) and strings (a,b,c,..). To make this differentiation, we use Data Types.

VBA data types can be segregated into two types

  • Numeric Data Types
Type Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to 2,147,483,648
Single 4 bytes -3.402823E+38 to -1.401298E-45 for negative values 1.401298E-45 to 3.402823E+38 for positive values.
Double 8 bytes -1.79769313486232e+308 to -4.94065645841247E-324 for negative values
4.94065645841247E-324 to 1.79769313486232e+308 for positive values.
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 12 bytes +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use +/- 7.9228162514264337593543950335 (28 decimal places)
  • Non-numeric Data Types
Data Type Bytes Used Range of Values
String (fixed Length) Length of string 1 to 65,400 characters
String (Variable Length) Length + 10 bytes 0 to 2 billion characters
Boolean 2 bytes True or False
Date 8 bytes January 1, 100 to December 31, 9999
Object 4 bytes Any embedded object
Variant(numeric) 16 bytes Any value as large as Double
Variant(text) Length+22 bytes Same as variable-length string

In VBA, if the data type is not specified, it will automatically declare the variable as a Variant.

Let see an example, on how to declare variables in VBA. In this example, we will declare three types of variables string, joining date and currency.

Step 1) Like, in the previous tutorial, we will insert the commandButton1 in our Excel sheet.

Excel VBA Data Types

Step 2) In next step, right-click on the button and select View code. It will open the code window as shown below.

Excel VBA Data Types

Step 3) In this step,

  • Save your file by clicking on save button Excel VBA Data Types
  • Then click on Excel icon Excel VBA Data Types in the same window to return the Excel sheet.
  • You can see the design mode is “on” highlighted in green

Excel VBA Data Types

Step 4) Turn off design mode, before clicking on command button

Excel VBA Data Types

Step 5) After turning off the design mode, you will click on commandButton1. It will show the following variable as an output for the range we declared in code.

  • Name
  • Joining Date
  • Income in curreny

Excel VBA Data Types

Constant in VBA

Constant is like a variable, but you cannot modify it. To declare VBA constants, you can use keyword Const.

There are two types of constant,

  • Built-in or intrinsic provided by the application.
  • Symbolic or user defined

You can either specify the scope as private by default or public. For example,

Public Const DaysInYear=365

Private Const Workdays=250

Download Excel containing above code

Download the above Excel Code

Variable Scope and Option Explicit

Where a variable is declared decides where it can be used, and one small setting at the top of a module prevents most typing mistakes. Understanding both keeps a growing program reliable.

Keyword Scope Lifetime
Dim (inside a Sub) Only the procedure it is declared in Reset each time the procedure runs
Static Only the procedure, but the value is kept Remembered between calls
Private (at module top) Every procedure in that module Lives while the workbook is open
Public (at module top) Every module in the project Lives while the workbook is open

๐Ÿ’ก Tip: Type Option Explicit as the first line of every module. It forces every variable to be declared with Dim, so a misspelled name is caught at compile time instead of silently creating a new empty Variant.

FAQs

A variable holds a value that the code can change while it runs. A constant holds a value that is fixed with Const and cannot be changed. Use a constant for a value that never varies, such as a tax rate.

Integer only holds values from -32,768 to 32,767, so a row count or a large number overflows it. Long holds far larger values, and Microsoft now recommends Long for whole numbers in modern VBA.

Variant is the default type when none is specified. It can hold a number, text, a date, or an object, which is flexible but uses more memory and runs slower. Declare a specific type where possible.

Yes. AI code assistants read how a variable is used and suggest a suitable type, such as Long for a counter or String for text. The developer confirms the choice and adds Option Explicit.

Yes. AI assistants flag a misspelled or undeclared variable, explain why Option Explicit reports it, and suggest the correct declaration. The developer applies the fix and recompiles the module.

Summarize this post with: