Home

VBA Keywords: Dim/As

  

Introduction

To declare a variable in the VBA language, you use the Dim keyword followed by a name for the variable, followed by the As keyword, followed by a data type. The formula to follow:

Dim VariableName As DataType

Here is an example:

Sub Exercise()
    Dim FirstName As Boolean
End Sub

In the same way, you can declare various variables, each on its line. Here are examples:

Sub Exercise()
    Dim FirstName As Integer
    Dim LastName As Double
End Sub

Instead of declaring each variable on its own line, you can declare various variables on the same line using one Dim keyword. The variables are separated by commas. This would be done as follows:

Sub Exercise()
    Dim FirstName As DataType, LastName As DataType
End Sub

Here is an example:

Sub Exercise()
    Dim FullName As String, DateHired As Date, IsMarried As Boolean
End Sub

 

 

   
 

Home Copyright © 2009-2016, FunctionX, Inc.