Lesson 25 of 60 – VBA Variables
42%

VBA Variables

A variable is a named storage location used to hold a value while a VBA program is running. Variables are useful when a program needs to store and work with data such as names, marks, prices, totals, and dates.

Variables make VBA programs more flexible because the stored value can be used, changed, and calculated during program execution.

Note: A variable has a name, a value, and usually a data type that describes what kind of data it can store.

1. What is a Variable?

A variable is a named location in memory used to store a value.

For example:

Dim marks As Integer

marks = 85

Here, marks is a variable and its value is 85.

2. Why Do We Use Variables?

Variables allow a program to store and manipulate information.

They are useful for:

  • Storing student names
  • Storing marks
  • Storing prices
  • Calculating totals
  • Storing counters
  • Storing dates
  • Making decisions based on values

3. Declaring a Variable

A variable is commonly declared using the Dim keyword.

Dim marks As Integer

Here:

  • Dim declares the variable.
  • marks is the variable name.
  • Integer is the data type.

4. The Dim Keyword

The Dim keyword is used to declare variables in VBA.

Dim name As String
Dim age As Integer
Dim salary As Double

Each variable has a name and a specified data type.

5. Assigning a Value to a Variable

After declaring a variable, you can assign a value to it using the equals sign (=).

Dim marks As Integer

marks = 90

The variable marks now contains the value 90.

6. Reading a Variable Value

A variable's value can be used in another VBA statement.

Sub ShowMarks()

    Dim marks As Integer

    marks = 85

    MsgBox marks

End Sub

The message box displays the value stored in marks.

7. String Variable

A String variable is used to store text.

Dim studentName As String

studentName = "Rahul"

MsgBox studentName

String values are normally written inside quotation marks.

8. Integer Variable

An Integer variable is used for whole-number values within the Integer data type's supported range.

Dim age As Integer

age = 20

MsgBox age

The value does not contain a decimal part.

9. Long Variable

The Long data type is useful for larger whole-number values.

Dim studentCount As Long

studentCount = 50000

MsgBox studentCount

Long is commonly useful for counters and larger numeric values.

10. Double Variable

A Double variable can store numbers that contain decimal values.

Dim percentage As Double

percentage = 87.50

MsgBox percentage

Double is useful for calculations involving decimal values.

11. Currency Variable

The Currency data type is designed for currency and fixed-point financial calculations.

Dim fee As Currency

fee = 1500.50

MsgBox fee

It is useful in applications involving fees, prices, payments, and other financial amounts.

12. Boolean Variable

A Boolean variable stores either True or False.

Dim passed As Boolean

passed = True

MsgBox passed

Boolean variables are useful when working with conditions.

13. Date Variable

A Date variable is used to store date and time information.

Dim admissionDate As Date

admissionDate = #9/30/2026#

MsgBox admissionDate

Date variables are useful for admission dates, payment dates, attendance dates, and other date-related operations.

14. Object Variables

VBA can also use variables that refer to Excel objects.

Dim ws As Worksheet

Set ws = Worksheets("Sheet1")

The variable ws now refers to a worksheet object.

The Set keyword is used when assigning an object reference.

15. Assigning Text to a Variable

Text values are assigned to String variables using quotation marks.

Dim course As String

course = "ADCA"

MsgBox course

The variable course stores the text ADCA.

16. Changing a Variable Value

A variable's value can be changed during program execution.

Dim marks As Integer

marks = 50

marks = 80

MsgBox marks

The final value of marks is 80.

17. Using Variables in Calculations

Variables can be used in mathematical calculations.

Dim a As Integer
Dim b As Integer
Dim total As Integer

a = 100
b = 200

total = a + b

MsgBox total

The result stored in total is 300.

18. Using Variables with Excel Cells

Variables can be used to read values from Excel cells.

Sub ReadMarks()

    Dim marks As Integer

    marks = Range("B2").Value

    MsgBox marks

End Sub

The value from cell B2 is stored in the marks variable.

19. Writing a Variable to a Cell

A variable can also be written into an Excel cell.

Sub WriteMarks()

    Dim marks As Integer

    marks = 85

    Range("B2").Value = marks

End Sub

The value stored in marks is written to cell B2.

20. Multiple Variables

A procedure can use multiple variables.

Sub StudentDetails()

    Dim studentName As String
    Dim age As Integer
    Dim marks As Integer

    studentName = "Amit"
    age = 20
    marks = 85

    MsgBox studentName & " - " & age & " - " & marks

End Sub

Different variables can store different types of information.

21. Variable Naming Rules

Variable names should follow VBA naming rules.

  • A variable name should begin with a letter.
  • It should not contain spaces.
  • It should not use reserved VBA keywords.
  • It should be meaningful.
  • Underscores can be used to improve readability.

Examples:

studentName
totalMarks
student_id

22. Good Variable Names

Meaningful variable names make programs easier to understand.

Good examples:

studentName
totalMarks
percentage
courseFee
studentCount

These names clearly indicate what information is being stored.

23. Poor Variable Names

Short or unclear variable names can make code difficult to understand.

x
a1
abc
temp1

Such names may be acceptable for very small calculations, but descriptive names are generally easier to maintain.

24. Variable Scope

Variable scope determines where a variable can be accessed.

For example, a variable declared inside a Sub Procedure is normally available only within that procedure.

Sub Test()

    Dim marks As Integer

    marks = 90

    MsgBox marks

End Sub

The marks variable belongs to this procedure.

25. Local Variables

A variable declared inside a procedure is commonly called a local variable.

Sub Calculate()

    Dim total As Integer

    total = 500

    MsgBox total

End Sub

The variable total is available within the procedure where it is declared.

26. Constants vs Variables

A variable can change its value during program execution. A constant is intended to represent a value that does not change during execution.

Variable Constant
Value can change Value is intended to remain fixed
Declared using Dim Declared using Const
Useful for changing data Useful for fixed values

27. Practical Student Example

Let's create a simple program using variables for student information.

Sub StudentInfo()

    Dim studentName As String
    Dim marks As Integer
    Dim percentage As Double

    studentName = "Rahul"
    marks = 425
    percentage = 85

    MsgBox studentName & vbCrLf & _
           "Marks: " & marks & vbCrLf & _
           "Percentage: " & percentage

End Sub

This example uses three variables to store student information.

28. Common Mistakes with Variables

Beginners commonly make these mistakes:

  • Using a variable without understanding its data type.
  • Using spaces in variable names.
  • Using unclear variable names.
  • Assigning incompatible values.
  • Forgetting to declare important variables.

Using meaningful names and appropriate data types makes VBA code easier to understand.

29. Complete Variable Workflow

A simple workflow for using a variable is:

  1. Choose a meaningful variable name.
  2. Choose an appropriate data type.
  3. Declare the variable using Dim.
  4. Assign a value to the variable.
  5. Use the variable in your program.
  6. Change the value when required.
Dim total As Integer

total = 500

MsgBox total

30. Complete Understanding of VBA Variables

A variable is a named storage location used to hold data during the execution of a VBA program. Variables can store text, numbers, dates, Boolean values, and references to Excel objects.

The Dim keyword is commonly used to declare variables. Choosing meaningful names and appropriate data types makes VBA programs easier to read, maintain, and debug.

Variables are essential for calculations, data processing, Excel automation, student management systems, reports, and many other practical VBA applications.

📌 Key Points

  • A variable stores a value during program execution.
  • The Dim keyword is commonly used to declare variables.
  • A variable has a name and a data type.
  • String stores text.
  • Integer and Long store whole-number values.
  • Double stores decimal numeric values.
  • Currency is useful for financial values.
  • Boolean stores True or False.
  • Date stores date and time information.
  • Variables can be used with Excel cells and calculations.
  • Meaningful variable names improve code readability.

🧠 Quick Quiz

Question: Which keyword is commonly used to declare a variable in VBA?