VBA 8. Arrays

We know very well that a variable is a container to store a value. Sometimes, developers are in a position to hold more than one value in a single variable at a time. When a series of values are stored in a single variable, then it is known as an array variable.

Suppose there are 3 courses vba excel and sql and you want to store these in variable. To do that we will have to create 3 variables


dim course1 as string

dim course2 as string

dim course3 as string

Now we can assign courses to variables

course1= “vba”

course2= “excel

course3= “sql”

With Arrays we can store multiple values in a single variable.

dim courses(0 to 2) or dim courses(2)  ‘ Declare array variable, this will create 3 memory locations  starting from location 0, location 1, location 2.  Values will be stored in these locations.

dim i as integer

for i=0 to 2

courses(i)= Range(“A” & i+3).value  ‘store values starting from Range A3 in array

next i

To Read values from Array

dim j as integer

for j=0 to 2

msgbox courses(j)

next j

Leave a Reply