| Start | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Multidimensional Arrays Multidimensional ArraysAs the name indicates, multi dimensional arrays are arrays that contain more than one dimension, usually two or three but it can have up to 32 dimensions. A multi array works like a matrix with various levels, take in example a comparison between one, two, and three Dimensions. One Dimension is your typical array, it looks like a list of elements. Two Dimensions would look like a Sudoku Grid or an Excel sheet, when initializing the array you would define how many rows and columns the array would have. Three Dimensions would start to look like Rubik's Cube, when initializing the array you would define rows and columns and layers/depths the array would have. Further dimensions could be thought as the multiplication of the 3D, so a 4D(1,3,3,3) would be two side-by-side 3D arrays. Two-Dimension ArrayCreating Resizing Changing Element Values Alternatively iterate through the coordinates use conditions to match values corresponding to the parameters needed: Reading Accessing the elements in the array can be done with a Nested Loop (iterating every element), Loop and Coordinate (iterate Rows and accessing columns directly), or accessing directly with both coordinates. Remember, it's always handy to keep an array map when using Multidimensional arrays, they can easily become confusion. Three-Dimension ArrayFor the 3D array, we'll use the same premise as the 2D array, with the addition of not only storing the Employee and Information but as well Building they work in. The 3D array will have the Employees (can be thought of as Rows), the Information (Columns), and Building that can be thought of as different sheets on an excel document, they have the same size between them, but every sheets has a different set of information in its cells/elements. The 3D array will contain n number of 2D arrays. Creating Resizing Resizing a 3D array is similar to resizing a 2D, first create a Temporary array with the same size of the original adding one in the coordinate of the parameter to increase, the first coordinate will increase the number of sets in the array, the second and third coordinates will increase the number of Rows or Columns in each set. The example below increases the number of Rows in each set by one, and fills those recently added elements with new information. Changing Element Values and Reading |
|||||||||||||||||||||||||
Dynamic Arrays (Array Resizing and Dynamic Handling) Dynamic ArraysAdding and reducing variables on an array dynamically is a huge advantage for when the information you are treating does not have a set number of variables. Adding Values DynamicallyYou can simply resize the Array with the In the example below we create an array and increase it by one more variable in each iteration while preserving the values already in the array. Removing Values DynamicallyWe can utilise the same logic to to decrease the the array. In the example the value "last" will be removed from the array. Resetting an Array and Reusing DynamicallyWe can as well re-utilise the arrays we create as not to have many on memory, which would make the run time slower. This is useful for arrays of various sizes.
One snippet you could use to re-utilise the array is to In the snippet below I construct an array with the values 1 to 40, empty the array, and refill the array with values 40 to 100, all this done dynamically. |
|||||||||||||||||||||||||
Jagged Arrays (Arrays of Arrays) Jagged Arrays NOT Multidimensional ArraysArrays of Arrays(Jagged Arrays) are not the same as Multidimensional Arrays if you think about them visually Multidimensional Arrays would look like Matrices (Rectangular) with defined number of elements on their dimensions(inside arrays), while Jagged array would be like a yearly calendar with the inside arrays having different number of elements, like days in on different months. Although Jagged Arrays are quite messy and tricky to use due to their nested levels and don't have much type safety, but they are very flexible, allow you to manipulate different types of data quite easily, and don't need to contain unused or empty elements. Creating a Jagged ArrayIn the below example we will initialise a jagged array containing two arrays one for Names and another for Numbers, and then accessing one element of each Dynamically Creating and Reading Jagged ArraysWe can as well be more dynamic in our approx to construct the arrays, imagine that we have a customer data sheet in excel and we want to construct an array to output the customer details. We will Dynamically construct an Header array and a Customers array, the Header will contain the column titles and the Customers array will contain the information of each customer/row as arrays.
The Result of the above snippet is an Jagged Array with two arrays one of those arrays with 4 elements, 2 indention levels, and the other being itself another Jagged Array containing 5 arrays of 4 elements each and 3 indention levels, see below the structure: To access the information you'll have to bear in mind the structure of the Jagged Array you create, in the above example you can see that the Now we'll read the information of the REMEMBER to keep track of the structure of your Jagged Array, in the example above to access the Name of a customer is by accessing |
|||||||||||||||||||||||||
| Declaring an Array in VBA Declaring an array is very similar to declaring a variable, except you need to declare the dimension of the Array right after its name: By default, Arrays in VBA are indexed from ZERO, thus, the number inside the parenthesis doesn't refer to the size of the array, but rather to the index of the last element Accessing ElementsAccessing an element of the Array is done by using the name of the Array, followed by the index of the element, inside parenthesis: Array IndexingYou can change Arrays indexing by placing this line at the top of a module: With this line, all Arrays declared in the module will be indexed from ONE. Specific IndexYou can also declare each Array with its own index by using the Dynamic DeclarationWhen you do not know the size of your Array prior to its declaration, you can use the dynamic declaration, and the Note that using the |
|||||||||||||||||||||||||
| Use of Split to create an array from a string Split Function returns a zero-based, one dimensional array containing a specified number of substrings. Syntax Split(expression [, delimiter [, limit [, compare]]])
Settings The compare argument can have the following values:
Example In this example it is demonstrated how Split works by showing several styles. The comments will show the result set for each of the different performed Split options. Finally it is demonstrated how to loop over the returned string array. |
|||||||||||||||||||||||||
Iterating elements of an array For...NextUsing the iterator variable as the index number is the fastest way to iterate the elements of an array: Nested loops can be used to iterate multi-dimensional arrays: For Each...NextA A Note that All 4 snippets above produce the same output: |
Read Article
https://develop.horje.com/learn/1434/reference