Excel VBA Declare Array
Declaration of array in VBA is very similar to that of variables it is done by the same dim statement or static public or private statement, the only difference in declaring an array and declaring a variable is that while declaring an array we have to provide a size of an array which is upper bound of the array and the lower bound of the array.
In VBA Code, we can declare a single variable array which can hold the number of variables instead of declaring single variables. This can help to reduce the number of line in the code.
The array is a kind of variable which can hold more than one value, unlike regular variables which can hold only one value at a time. The array is an advanced version of declaring variables in VBA. For an example imagine a situation where you want to assign 5 students names to variables and in general practice, we declare five variables for all the five variables we assign individual student name one by one, below is the example code of the same.
Sub Array_Example() Dim Student1 As String Dim Student2 As String Dim Student3 As String Dim Student4 As String Dim Student5 As String End Sub
Instead of declaring so many variables how about the idea of declaring a single variable array which can hold all the student names.
Yes, this is possible by declaring the array in VBA.
To declare we need not do any special VBA coding rather we need to follow simple concepts.
First, start the subprocedure.
Sub Array_Example() End Sub
Now, as usual, declare a variable as a string.
Sub Array_Example() Dim Student As String End Sub
Once the variable is declared now make sure how many values it should hold. In this case, I want to store five students’ names, so now we need to fix the array size i.e. 1 to 5. Supply the same thing to variable in brackets.
Sub Array_Example() Dim Student(1 To 5) As String End Sub
Now for this single variable, we can store 5 student names.
Sub Array_Example() Dim Student(1 To 5) As String Student(1) = "John" Student(2) = "Peter" Student(3) = "Ricky" Student(4) = "Michael" Student(5) = "Anderson" End Sub
Look how many lines we have reduced by declaring the variable as an array. This is one way of doing we can still shorten this code by enclosing this inside the loops in VBA.
Now for an example, the same five names I have in worksheet cells.3
Now I want to show these numbers in the message box in VBA, ok let’s declare one more variable for loops as Integer data type.
Sub Array_Example() Dim Student(1 To 5) As String Dim K As Integer End Sub
As usual, I have retained the array variable as 1 to 5 size.
Now open FOR NEXT loop in VBA and since we have five names enter the limit as 1 to 5.
Sub Array_Example() Dim Student(1 To 5) As String Dim K As Integer For K = 1 To 5 Next K End Sub
To assign values to array variable we need not follow the previous way of showing Student(1), Student(2) like this for numbers position supply loops variable “k”.
Sub Array_Example() Dim Student(1 To 5) As String Dim K As Integer For K = 1 To 5 Student(K) = Next K End Sub
For this array variable we need the values from the worksheet, so using CELLS property get the values from the worksheet.
Sub Array_Example() Dim Student(1 To 5) As String Dim K As Integer For K = 1 To 5 Student(K) = Cells(K, 1).Value Next K End Sub
Now through message box show the value of the array variable.
Sub Array_Example() Dim Student(1 To 5) As String Dim K As Integer For K = 1 To 5 Student(K) = Cells(K, 1).Value MsgBox Student(K) Next K End Sub
Now run the code, in the message box, we will see the first name. Again press Ok to see the second name. Like this by pressing Ok, we can see all the five names.
Example #2 – Two Dimensional Arrays
We have seen above how the array works, now we will see to dimensional arrays. Two-dimensional arrays concentrate on both rows and columns.
In the above example, we have determined the array’s size as 1 to 5, this either concentrates on rows or columns.
By using two-dimensional arrays we can concentrate on both rows and columns. For this, we need to enclose two loops.
First, define the variable then late we will decide about the size of the array.
Sub Two_Array_Example() Dim Student As String End Sub
First, decide row size then decide the column length.
Sub Two_Array_Example() Dim Student(1 To 5, 1 To 3) As String End Sub
For this, I have structured the data for student name, marks and grade status.
Now come back to the coding window.
Declare two more variables for a loop.
Sub Two_Array_Example() Dim Student(1 To 5, 1 To 3) As String Dim K As Integer, J As Integer End Sub
Now enclose the loop as shown below.
Sub Two_Array_Example() Dim Student(1 To 5, 1 To 3) As String Dim k As Integer, J As Integer For k = 1 To 5 For J = 1 To 3 Worksheets("Student List").Select Student(k, J) = Cells(k, J).Value Worksheets("Copy Sheet").Select Cells(k, J).Value = Student(k, J) Next J Next k End Sub
What this will do is it will copy the data from the “Student List” sheet and paste in “Copy Sheet”.
Things to Remember
- The array is a vast concept, this is just an introductory part.
- You need advanced coding skills to understand the array declaration.
- The more you use arrays in your code the more you will get used to it.
This has been a guide to VBA Declare Array. Here we learn how to declare one dimensional and two-dimensional arrays in vba along with examples and downloadable excel template. Below are some useful excel articles related to VBA –