In the previous lesson, we learned about VBA Variables. A variable stores a value during program execution. A data type tells VBA what kind of value a variable is designed to store.
Choosing an appropriate data type helps make VBA programs easier to understand and can help VBA handle data correctly.
A data type defines the kind of data that a variable can store.
For example:
Dim studentName As String
Dim marks As Integer
Here, String is used for text and Integer is used for whole-number values.
Data types help VBA understand how a variable should be handled.
They help you:
A data type is specified when declaring a variable.
Dim name As String
Dim age As Integer
Dim percentage As Double
The keyword As is used to specify the data type.
The String data type is used to store text.
Dim studentName As String
studentName = "Rahul"
MsgBox studentName
Text values are normally enclosed in quotation marks.
The Integer data type is used to store whole-number values within the Integer range.
Dim age As Integer
age = 20
MsgBox age
An Integer does not contain a decimal part.
The Long data type is used for larger whole-number values than Integer.
Dim studentCount As Long
studentCount = 50000
MsgBox studentCount
Long is useful for counters and other whole-number values that may be larger than the Integer range.
The Single data type stores single-precision floating-point numbers. It can be used when decimal values are required.
Dim temperature As Single
temperature = 36.5
MsgBox temperature
Single is suitable for many calculations where single-precision decimal values are sufficient.
The Double data type stores double-precision floating-point numbers. It is commonly used for decimal calculations.
Dim percentage As Double
percentage = 87.75
MsgBox percentage
Double is useful when more precision is needed for decimal calculations.
The Currency data type is designed for currency and fixed-point financial calculations.
Dim courseFee As Currency
courseFee = 10500.50
MsgBox courseFee
It is useful for fees, prices, payments, salaries, and other financial values.
The Boolean data type stores one of two logical values: True or False.
Dim passed As Boolean
passed = True
MsgBox passed
Boolean variables are commonly used with conditions.
The Date data type is used to store date and time values.
Dim admissionDate As Date
admissionDate = #9/30/2026#
MsgBox admissionDate
Date variables are useful for attendance, admission, payment, and report applications.
The Byte data type stores small whole-number values from 0 to 255.
Dim level As Byte
level = 100
MsgBox level
Byte is useful when the value is known to stay within its supported range.
The Decimal type provides a fixed-point numeric representation with
high precision. In VBA, Decimal is available as a subtype of the
Variant data type rather than as a normal standalone declaration
such as Dim amount As Decimal.
For example:
Dim amount As Variant
amount = CDec(12345.6789)
MsgBox amount
The CDec function converts a value to the Decimal subtype.
The Variant data type can contain different kinds of values. It is the default data type when a variable is declared without an explicit type.
Dim value As Variant
value = "Hello"
value = 100
MsgBox value
Variant is flexible, but using a specific data type can make the intended type of data clearer.
The Object data type can refer to an object.
Dim obj As Object
Set obj = Worksheets("Sheet1")
The Set keyword is used when assigning an object reference.
VBA also provides specific object types such as Worksheet.
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
ws.Range("A1").Value = "Hello"
This makes it clear that ws refers to a worksheet.
The Workbook data type can be used for an Excel workbook object.
Dim wb As Workbook
Set wb = ThisWorkbook
MsgBox wb.Name
Here, wb refers to the workbook containing the VBA project.
The Range object type is useful when working with cells or groups of cells.
Dim rng As Range
Set rng = Range("A1:B5")
rng.Value = "Excel"
The variable rng refers to the selected range.
Choose a data type according to the kind of information the variable will store.
| Data | Possible Data Type |
|---|---|
| Student name | String |
| Age | Integer |
| Large counter | Long |
| Percentage | Double |
| Course fee | Currency |
| Pass/Fail status | Boolean |
| Admission date | Date |
A variable declaration can specify both the variable name and its data type.
Dim studentName As String
Dim marks As Integer
Dim percentage As Double
Dim fee As Currency
Dim passed As Boolean
This makes the purpose of each variable easier to understand.
You can declare multiple variables of the same data type in one statement.
Dim firstName As String
Dim lastName As String
Dim course As String
Each variable is declared as a String.
You can also declare variables on one line:
Dim firstName As String, lastName As String
VBA provides conversion functions when you need to convert a value from one type to another.
Common conversion functions include:
Suppose a number is stored as text and needs to be converted to an Integer.
Sub ConvertValue()
Dim value As String
Dim number As Integer
value = "100"
number = CInt(value)
MsgBox number
End Sub
The CInt function converts the text value into an Integer.
Excel cells can contain different kinds of information, and VBA variables can be used to work with that information.
Sub ReadStudent()
Dim studentName As String
Dim marks As Integer
studentName = Range("A2").Value
marks = Range("B2").Value
MsgBox studentName & " - " & marks
End Sub
The variables are chosen according to the expected data.
Choosing a suitable numeric data type is important when performing calculations.
Sub CalculatePercentage()
Dim marks As Double
Dim total As Double
Dim percentage As Double
marks = 425
total = 500
percentage = marks / total * 100
MsgBox percentage
End Sub
The Double type is appropriate here because the result can contain decimal values.
Beginners can face problems when a variable is given a value that does not fit the intended data type.
Common mistakes include:
Let's create a simple student program using different data types.
Sub StudentDetails()
Dim studentName As String
Dim age As Integer
Dim marks As Integer
Dim percentage As Double
Dim fee As Currency
Dim passed As Boolean
studentName = "Amit"
age = 20
marks = 425
percentage = 85
fee = 10500
passed = True
MsgBox "Name: " & studentName & vbCrLf & _
"Age: " & age & vbCrLf & _
"Marks: " & marks & vbCrLf & _
"Percentage: " & percentage & vbCrLf & _
"Fee: " & fee & vbCrLf & _
"Passed: " & passed
End Sub
This example demonstrates several common VBA data types in one program.
A simple method for selecting a data type is:
A data type defines the kind of value that a VBA variable is designed to store. VBA provides data types for text, numbers, dates, logical values, and object references.
Common data types include String, Integer, Long, Single, Double, Currency, Boolean, Date, Byte, Variant, and Object. Excel-specific object types such as Worksheet, Workbook, and Range can also be used when working with Excel objects.
Choosing the correct data type makes VBA programs clearer and helps you work with data appropriately.
Question: Which VBA data type is commonly used to store text?