Saturday, 1 December 2012

How to Declare an Array with Dim Statement in VBA Arrarys

Basic syntax of Declaring an Array with Dim Statement
An array is a set of sequentially indexed elements having the same intrinsic data type. Each element of an array has a unique identifying index number. Changes made to one element of an array don't affect the other elements. 

Before signing values to an array, the array needs to be created. You can declare the array by using the Dim statement.

For example, to declare a one-dimensional array with 5 elements, type the following:
Dim Arr(4)

The element’s index of the array starts from 0 unless Option Base 1 is specified in the public area (area outside of the sub procedure). If Option Base 1 is specified, the index will start from 1.

The following example assigns values to the array and displays all values in a message box :
Option Base 1
Sub Declare_Array( )
Dim Arr(5)

Arr(1) = “Jan”
Arr(2) = “Feb”
Arr(3) = “Mar”
Arr(4) = “Apr”
Arr(5) = “May”

Msgbox Arr(1) & "-" & Arr(2) & "-" & Arr(3) & "-" & Arr(4) & "-" & Arr(5)
End Sub

* The number inside the array, i.e. Arr(1), is the index. One (1) is the index of the first element in the array.

No comments:

Post a Comment

Hi User, Thank You for Visiting My Blog. Please Post Your Feedback/Comments/Query.

Subscribe to Blog Posts by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts