
A better option would be to use a Dynamic array, which is an array that can be sized and re-sized as many times as you like, during the execution of a macro. One way to solve this is to declare a huge array, in an attempt to cover the maximum possible size needed, but this would use up an unnecessarily large amount of memory and could slow down your program. However, in many cases, you may not know how big an array is going to be before run time. In the above examples, the arrays all have fixed dimensions. This is understood to be a two-dimensional array in which the first dimension has 32 entries, indexed from 0 to 31 and the second dimension has 6 entries, indexed from 0 to 5. However, if we omit the start indices from both dimensions, as follows: Dim Jan_Sales_Figures(31, 5) As Currency As shown in the previous example, a two-dimensional array is declared by separating the dimension indices by a comma: Dim Jan_Sales_Figures(1 To 31, 1 To 5) As Currency The same rules are applied to declarations of multi-dimensional Visual Basic arrays. Base 2: The array is a fixed-size data structure while ArrayList is not. Therefore array members are accessed using, while ArrayList has a set of methods to access elements and modify them. ArrayList is part of the collection framework in Java. Then the VBA compiler will understand this to be an array of 20 variables, which are indexed from 0 to 19. Base 1: An array is a basic functionality provided by Java. In fact, the default form of array indexing is to start at 0, so if you omit the start index from the declaration, and simply declare the array as: Dim Team_Members(19) As String However, we could also decide to index the 20 array variables from 0 to 19, in which case the declaration would be: Dim Team_Members(0 To 19) As String This declaration tells the VBA compiler that the array 'Team_Members' has 20 variables, which are referenced by indices 1 to 20. As seen above, a one-dimensional array can be declared as follows: Dim Team_Members(1 To 20) As String The above sections have already given some examples of Visual Basic Array declarations, but it is worth discussing this further. by adding further dimensions into the declaration and using a further index to reference the array entries. You can declare arrays with 3 or more dimensions in the same way. For example, the sales figures for Team2 on January 15th would be referenced as: Jan_Sales_Figures(15, 2) In order to access the entries in the array 'Jan_Sales_Figures', you need to use two indices, refering to the day of the month and the team number. You would then declare the array as follows: Dim Jan_Sales_Figures(1 To 31, 1 To 5) As Currency You would need a 2-dimensional array, consisting of 5 sets of figures over 31 days. An array having two dimensions can be thought of as a grid of values.įor example, imagine that you want to store daily sales figures for the month of January, for 5 different teams. However, arrays can have multiple dimensions. The Visual Basic Arrays discussed above are one-dimensional, in that they refer to one list of Names. Multi-Dimensional Excel Visual Basic Arrays Cells(i, 1).Value = Team_Members(i) Next iĮven with just 20 names, the advantages of using an Array are clear, but imagine if you had 1,000 names to store! And imagine you wanted to store Surnames separately from Forenames! It would soon become almost impossible to handle this amount of data without the use of Arrays in your VBA code.
