×

Declaring a Variable in VBA

Declaring a Variable

A variable is broadly described as a storage location combined with a name and representing a specific value. Declaring a variable is instructing the computer to reserve space in memory for later use.

Syntax for Declaring Variable

Scope VariableName As DataType

Steps for Declaring Variables 

In VBA, declaring has three steps:

  1. Set a Scope for the variable - Every variable has a scope associated with it. The scope of a variable is defined at the time the variable is declared. The available scope for a variable can be the either of three scopes i.e., Dim, Private, and Public.

2. Set a name for the variable – Variables are allocated with a name so as the can identify the variables for future use. Two variables within the same procedure can not have identical names. There are few naming convictions which are as follows:

  • The first character should be alphabetic.
  • The following characters can be alphabetic, numeric or punctuation character excluding * . , # $ % & !
  • Spaces or periods abandoned.
  • Underscore character is also used to increase readability and distinguish two words
  • The name can have a maximum of 254 characters.
  • Variable names cannot be similar to VBA keywords, unlike Sub, Function, Integer, Array, etc.,.

3. Set a type for the variable – Data Types defines the type of data you’re going to store in the variable. The various data types available are Boolean, Byte, Integer, Long, Currency, Single, Double, Date, String, Object, and Variant.

Program 1

Sub DeclaringVariableExample()
                ‘Declaring the variable ‘a’ with scope= Dim and data type = Integer
Dim a As Integer
a = 12
MsgBox "The value of first variable is " & a
'MsgBox will return the Output: 2
‘Declaring the variable ‘b’ with scope= Dim and data type = Long
Dim b As Long
b = a + 2
MsgBox "The value of second variable is " & b
'Outputs: 4
‘Declaring the variable ‘c’ with scope= Dim and data type = String
Dim c As String
c = "Hello World"
MsgBox "The value of third variable is " & c
'Outputs: Hello, world!
End Sub

Output

The Value of a is 2

Declaring a Variable in VBA

The value of b is 4

Declaring a Variable in VBA

The value of c is Hello, world!

Declaring a Variable in VBA

Declaring Multiple Variables

Program 2

Sub DeclaringMultipleVariableExample()
'declaring multiple variables
Dim X As Integer, Y As Integer, Z As Integer
X = 2
MsgBox "The value of first variable X is " & X
'Outputs: 2
Dim b As Long
Y = X + 2
MsgBox "The value of second variable Y is " & Y
'Outputs: 4
Dim c As String
Z = Y + 2
MsgBox "The value of third variable Z is " & Z
'Outputs: 6
End Sub

Output

The value of X is 2

Declaring a Variable in VBA

The value of Y is 4

Declaring a Variable in VBA

The value of Z is 6

Declaring a Variable in VBA

Related Topics

VBA Regex

What is a Regex? Regex stands for Regular Expression is basically a pattern matching strings within another string. They are supported in many languages, including .net, C++, Python, etc. They are...

5 minutes read.

Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.

Excel VBA CInt Function

VBA CInt Function: The VBA Cint function converts the specified expression into an Integer. Syntax Cint (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to an Integer wherein...

1 minute read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA FormatNumber Function

VBA FormatNumber Function: The FormatNumber function in VBA is used to apply a number format to a numeric expression, and it returns the result as a string. Syntax FormatNumber (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.

Excel VBA IsArray Function

VBA IsArray Function: The IsArray function in VBA returns a Boolean, showing whether the given variable is an Array or not. Syntax IsArray (VarName) Parameter VarName (required)- This parameter represents the variable that you want...

1 minute read.

Excel VBA CVar Function

VBA CVar Function: The CVar function in VBA converts an expression into a Variant data type. Syntax CVar (Expression) Parameter Expression (required) – This parameter represents the expression that that you want to convert...

1 minute read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.

Excel VBA Tutorial

What is VBA? Introduction to Excel VBA: Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate operations in applications, such as Excel, Word, PowerPoint, etc. It...

5 minutes read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

Excel VBA Error Function

VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code. Syntax Error ([ErrorNumber]) Parameter ErrorNumber (optional) – This parameter represents the required error number. By default, the...

1 minute read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

VBA Object Required

What is Object Required Error? VBA Object Required is a run time error which occurs when the user does not define a valid object qualifier, or the assigned object doesn’t exist in the...

6 minutes read.