Arrays are simply sets of values or elements stored in continuous memory. This is a data structure that stores a group of elements with the same data type.

This type of structure is very useful in the programming world because it helps to organize, sort and retrieve data.

Later in this article I will show you how to create a multi-dimensional image, but first I will give you a few examples to make sure you understand all the concepts.

One-dimensional table

It is best to think of this structure as a list of elements arranged one below the other. A table is a single column of items of the same data type. The table index always starts at 0, the elements are recognized by their corresponding indexes.

Here is an example of a VBA chart that contains a shopping list. You can see that there are five materials stored in indexes 0 to 4 (5 materials in total). The value of the incoming array is the index value with which the elements of the array begin, and the value of the outgoing array is the index value with which the list of arrays ends.

Sub array_snacks()

Variable declaration Dim arr_snacks1(5)

Elements of table
arr_snacks1(0) = Burger
arr_snacks1(1) = Pizza
arr_snacks1(2) = Pasta
arr_snacks1(3) = Pasta
arr_snacks1(4) = Sandwich

Print all elements of the array
For i = LBound(arr_snacks1) To UBound(arr_snacks1)
Debug.Print arr_snacks1(i)
Next

Last Subsection

Go away:

Hamburger

Pizza

Macaroni

Noodles

Sandwich

Two-dimensional table

These tables can be seen as values distributed across the rows and columns in two dimensions. These two dimensions can be indicated by X and Y coordinates.

Notes

To declare an array that contains more than one dimension, we use commas to separate the dimensions.

E.G.: Dim arr_sample(3,2) as an integer

It is a table of 3 rows and 2 columns that can contain integer values.

Example of a two-dimensional table

Sub array_2d_snacks()

Specify and define the size of table
. 4 means 5 rows from 0 to 4, and 1 means 2 columns from 0 to 1
Dim arr_sna(4, 1) Optional

Initialize array elementsarr_sna(0, 0) = Citizenarr_sna(0, 1) = 5arr_sna(1, 0) = Pastaarr_sna(1,arr_sna(2, 0) = 7arr_sna(2, 0) = Sandwicharr_sna(2, 1) = 9arr_sna(3, 0) = Pastaarr_sna(3, 1) = 6arr_sna(4, 0) = Pizzaarr_sna(4, 1) = 10

Print all elements
For i = LBound(arr_sna) To UBound(arr_sna)

Debug.print food: & arr_sna(i, 0)
Debug.print price: & arr_sna(i, 1)
Debug.print End entry
Next

Last Subsection

You can see below the structure of the 2D table defined by the above code.

  1. Column 0 – Capital and
  2. Column 1 – Price of the product
Column 0 Column 1
Line 0 Hamburger 5
Row 1 Noodles 7
Row 2 Sandwich 9
Row 3 Macaroni 6
Row 4 Pizza 10

alt=Output of a two-dimensional array data-orig-width=960 data-orig-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />.

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Multidimensional networks

Networks with more than one dimension are called multidimensional networks. So the 2D matrices discussed above also fall into this category.

Usually two or three dimensions can meet our programming objectives. However, an array can have up to 32 dimensions!

Explanatory notes – specified

As mentioned earlier in this article, dimensions are separated by commas when declaring an object in the table. There is another way to declare an array object – by specifying lbound and ubound. Let me explain with an example.

Dim arr_sample ( 1 to 5 , 1 to 2 ) as string

In this example, we say that the index of elements in the first dimension should start at 1, not 0, and scroll through the index 5 with 5 elements. This means that lbound equals 1 and ubound equals 5 for the lines. The second dimension means a column. The index must start with 1 and end with 2. The band is equal to 1 and the band is equal to 2.

Complete example of a 2D table with nesting for Cycle.

  • Here is an example of a two-dimensional table with the names of students and their exam results.

Sub array_2d_demo()

1 to 5 means 5 rows from 1 to 5 and 1 to 2 means 2 columns from 1 to 2
Dim arr_stu(1 to 5, 1 to 2) Optional

Initializing table elements

arr_stu(1, 1) = Dave
arr_stu(1, 2) = Failure
arr_stu(2, 1) = Trumpo
arr_stu(2, 2) = Success
arr_stu(3), 1) = Vincent
arr_stu(3, 2) =
arr_stu(4, 1) = Rose Maria
arr_stu(4, 2) =
arr_stu(5, 1) = Eliza
arr_stu(5, 2) = Rejection

Debug.Print arr_stu(i, j)Next jDebug.Print end of recordNext

Last Subsection

alt=Installed for loop output data-original width=960 data-original height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357834_793_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

  • Here is an example of a three-dimensional table.

You can compare it to a Rubik’s Cube. It is a two-dimensional painting with layers in front and behind (one behind the other). Suppose the weather stores student information.

alt=Output of many dimensions data-original width=960 data-original height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357835_860_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />.

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Sub array_2d_demo()

Specifying and defining the size of the table
‘ 1 to 5 means 5 rows 1 to 5 and 1 to 2 means 2 columns 1 to 2
‘ 1 to 2 because the last parameter of the table object specifies 2 levels of a 2-dimensional table, making it a 3-dimensional table ( 3-dimensional ).
Dimm_stu(1 – 5, 1 – 2, 1 – 2) as variant

Initialisation of elements of array
arr_stu(1, 1, 1) = Dave
arr_stu(1, 1, 2) = Stu_001
arr_stu(1, 2, 1) = Fail ‘ First result of periodic test Stu_001
arr_stu(1, 2, 2) = Pass ‘ Second result of periodic test Stu_001

arr_stu(2, 1, 1) = Diane
arr_stu(2, 1, 2) = Stu_002
arr_stu(2, 2, 1) = fail ‘ First result of periodic test Stu_002
arr_stu(2, 2, 2) = pass ‘ Second result of periodic test Stu_002

arr_stu(3, 1, 1) = Elizabeth
arr_stu(3, 1, 2) = Stu_003
arr_stu(3, 2, 1) = pass ‘ First result of periodic test Stu_003
arr_stu(3, 2, 2) = pass ‘ Second result of periodic test Stu_003

arr_stu(4, 1, 1) = Ricard
arr_stu(4, 1, 2) = Stu_004
arr_stu(4, 2, 1) = Failing ‘ First result of periodic test Stu_004
arr_stu(4, 2, 2) = Failing ‘ Second result of periodic test Stu_004

arr_stu(5, 1, 1) = trump
arr_stu(5, 1, 2) = Stu_005
arr_stu(5, 2, 1) = pass ‘ First result of periodic test Stu_005
arr_stu(5, 2, 2) = fail ‘ Second result of periodic test Stu_005

For i = 1 to 5
For j = 1 to 2
For k = 1 to 2
Debug.Print arr_stu(i, j, k)
Next k
Next j
Debug.Print end of record
Next
End of Sub

Here is the structure of the three-dimensional table presented above.

1. Front position
Column 1 Column 2
Rule 1 Dave Failure
Rule 2 Diana Failure
Rule 3 Elizabeth Continue with
Rule 4 Ricarda Failure
fifth line Assets Continue with
2. Front position
Column 1 Column 2
Rule 1 Stu_001 Continue with
Rule 2 Stu_002 Continue with
Rule 3 Stu_003 Continue with
Rule 4 Stu_004 Failure
fifth line Stu_005 Failure

Change of size of tables

Resetting an array is nothing more than rescaling an array that has already been declared with or without dimensions. ReDim is the key word used for this.

Set the size of the table immediately after the declaration

Declaring an array of variables
Dim arr() as variable

Get the length of table
ReDim arr(8)

In this piece of code, we simply declared an array variable without specifying its size. Therefore, the keyword ReDim is used in the next line to determine its size. Now 8 variant elements can be stored in the Array object. They are indexed from 0 to 7.

Increase the size of table

Here is a piece of code that uses the ReDim keyword to change the size of the array. Resizing removes the contents of the table. So the display board is empty again from index 0 to 4. If necessary, the values must be reset with table indices.

Sub array_cars()

Variable declaration
Dim arr_cars1() Variable
ReDim arr_cars1(5)

Initializing the elements of an array
arr_cars1(0) = Benz
arr_cars1(1) = Maruthi
arr_cars1(2) = Hyundai
arr_cars1(3) = Ford
arr_cars1(4) = Nissan

Increase the array
ReDim arr_cars1(6)
arr_cars1(5) = Toyota

For i = LBound(arr_cars1) To UBound(arr_cars1)
Debug.Print arr_cars1(i)
Next

Last Subsection

Go away:

alt=Events – data-orig-width=960 data-orig-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357836_768_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

If we need to add elements to the array again, we need to insert this fragment again between the ReDim rows arr_cars1(6) and arr_cars1(5) = Toyota.

backseat1(0) = Benz
backseat1(1) = Maruthi
backseat1(2) = Hyundai
backseat1(3) = Ford
backseat1(4) = Nissan

Reducing the size of the network

The size of the table can be reduced after assigning values to the table elements, but all existing values will be removed.

The following example demonstrates this:

Sub array_cars()

Declare variable
Dim arr_cars1() Variant
ReDim arr_cars1(5)

Initialise the elements of the array (all six elements are assigned a value)
arr_cars1(0) = Benz
arr_cars1(1) = Maruthi
arr_cars1(2) = Hyundai
arr_cars1(3) = Ford
arr_cars1(4) = Nissan
arr_cars1(5) = Toyota

Restrictions on printing before resizing the array
Debug.Print UBound(arr_cars1)
Debug.Print LBound(arr_cars1)

Reduce array
ReDim arr_cars1(3)
Debug.Print Boundaries after resizing array
Debug.Print UBound(arr_cars1)
Debug.Print LBound(arr_cars1)

Reset the values again if you resize and delete the values
arr_cars1(0) = Benz
arr_cars1(1) = Maruthi
arr_cars1(2) = Hyundai
arr_cars1(3) = Ford

Debug.Print restrictions after resizing the array and reassigning values
Debug.Print UBound(arr_cars1)
Debug.Print LBound(arr_cars1)
Debug.Start printing
‘ Print all elements of the first array
For i = LBound(arr_cars1) To UBound(arr_cars1)
Debug.Print arr_cars1(i)
Next
Debug.Print End Printing
End Sub

The code explains what each line does and how the ReDim statement affects the size of the table.

alt=data-original-width=960data-original-height=540data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357837_528_How-Do-You-Redim-a-Multidimensional-Array.pngданные-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

ReDim keyword with multidimensional arrays

In the case of a multidimensional array, the ReDim keyword can help in two ways.

  1. Increasing or decreasing the number of dimensions
  2. Increasing or decreasing the length of dimensions

Corrected tables

These are tables whose dimensions are specified when declaring with the Dim keyword.

E.G.:

Dim Cars_arr(3,5) as string

Digital cars (1 to 4, 1 to 3) in chain form

Dynamic networks

These are arrays whose size is not specified when declared with the Dim keyword.

E.G.:

Dim fibero_arr() as an integer

Resizing examples

Example of a fixed table where the last dimension cannot be changed:

Sub array_2d_redim_demo()

Declaration and dimensioning of an array
Dim arr_sna(4, 1) as variant

Initialize array elementsarr_sna(0, 0) = Citizenarr_sna(0, 1) = 5arr_sna(1, 0) = Pastaarr_sna(1,arr_sna(2, 0) = 7arr_sna(2, 0) = Sandwicharr_sna(2, 1) = 9arr_sna(3, 0) = Pastaarr_sna(3, 1) = 6arr_sna(4, 0) = Pizzaarr_sna(4, 1) = 10

ReDim arr_sna(4, 2)

Print all elements
For i = LBound(arr_sna) To UBound(arr_sna)

Debug.Print food: & arr_sna(i, 0)
Debug.Print s : & arr_sna(i, 1)
Debug.Print End entry
Next

Last Subsection

alt=Compilation Error : The table is already defined. data-orig-width=960 data-orig-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357838_116_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Furthermore, if the redim declaration is used in a fixed array immediately after the declaration, the above error is reset.

Declare and dimension an array
Dim arr_sna(4, 1) As variant
ReDim arr_sna(4, 2)

Dynamic table with variable size:

Sub array_2d_redim_demo()

Step 1 : Explanation of the dynamic array
Dim arr_sna() Variant

Step 2 : Determine the size of the array by reentering the keyword
ReDim arr_sna(4, 1).

Step three: Initialize the elements of the table for the first time.

arr_sna(0, 0) = Hamburger
arr_sna(0, 1) = 5
arr_sna(1, 0) = Noodles
arr_sna(1, 1) = 7
arr_sna(2), 0) = Sandwich
arr_sna(2, 1) = 9
arr_sna(3, 0) = Noodles
arr_sna(3, 1) = 6
arr_sna(4, 0) = Pizza
arr_sna(4, 1) = 10

Step 4: print and check all data
Debug and print Step 4
Debug and print food product and price
For i = LBound(arr_sna) to UBound(arr_sna)
Debug and print arr_sna(i, 0) & arr_sna(i, 0) & arr_sna(i, 1)
Next

Step 5 : Replace the size of the same array by trying to change both sizes
ReDim arr_sna(7, 2).

Step 6 : All data will be deleted. Confirm by printing
Debug.Print Step 6
Debug.Print Food & Price & Rating
For i = LBound(arr_sna)
Debug.Print arr_sna(i, 0) & arr_sna(i, 0) & & arr_sna(i, 1) & & arr_sna(i, 2)
Next

Step 7 : Initialize table entries as all data is removed.arr_sna(0, 0) = Burgerargg_sna(0, 1) = 5arr_sna(0, 2) = 3arr_sna(1, 0) = Noodlegg_sna(1, 1) = 7arr_sna(1, 2) = 4arr_sna(2) = 3arr_sna(1), 0) = Noodle_sna(1, 1) = 7arr_sna(1, 2) = 4arr_sna(2), 0) = Sandwicharr_sna(2, 1) = 9arr_sna(2, 2) = 5arr_sna(3, 0) = Pastaarr_sna(3), 1) = 6arr_sna(3, 2) = 4arr_sna(4, 0) = Risagg_sna(4, 1) = 12arr_sna(4, 2) = 2arr_sna(5, 0) = Risagg_sna(5, 1) = 8arr_sna(5), 2) = 1arr_sna(6, 0) = curd circlearr_sna(6, 1) = 6arr_sna(6, 2) = 5arr_sna(7, 0) = sweet puddingarr_sna(7, 1) = 4arr_sna(7, 2) = 4

Debugging the printing of food and price and rankingFor i = LBound(arr_sna) to UBound(arr_sna)Debugging the printing of food and price and rankingFor i = LBound(arr_sna) to UBound(arr_sna)Debugging the printing of food and price and rankingNext

Last Subsection

Exit in the direct window

alt=Original data-width=960 data-original-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357840_620_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Change the number of dimensions:

Sub arr_redim_demo()

Step 1 : Declare array
Dim even_nos() as option

Step 2 : Determine the size and number of dimensions
ReDim even_nos(4, 1).

Step three: Initializing values in an array

even_nos(0, 0) = 2even_nos(0, 1) = one digitven_nos(1, 0) = 4even_nos(1, 1) = one digitven_nos(2, 0) = 6even_nos(2, 0) = 6 even_nos(2, 1) = one digit_nos(3, 0) = 8 even_nos(3, 1) = one digit_nos(4, 0) = 10 even_nos(4, 1) = two digits

Step 5: Print all elements once
Debug.Print Step 5
For i = LBound(even_nos) To UBound(even_nos)
Debug.Print even_nos(i, 0) & even_nos(i, 1)
Next
Debug.Print End of printing

Step 6 : Reset the size and number of dimensions with the keyword renamed
ReDim even_nos(5).

Step 7: Try printing the table to make sure it is empty because of repeated keyword entry
Debug.Print Step 7
For i = LBound(even_nos) To UBound(even_nos)
Debug.Print even_nos(i)
Next
Debug.Print End Printing

Step 8 : Redistribution of values according to the new size
even_nos(0) = 12
even_nos(1) = 14
even_nos(2) = 16
even_nos(3) = 18
even_nos(4) = 20
even_nos(5) = 22

Step 9 : Reprint according to the new dimensions
Debug.Print Step 9
For i = LBound(even_nos) To UBound(even_nos)
Debug.Print even_nos(i)
Next
Print end Debug.Print

Last Subsection

The result is shown below.

alt=Release after reduction data-orig-width=960 data-orig-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357841_775_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Store Keyword

Preserve is a keyword that can be used to preserve data in a table if its size is changed later.

Restriction : If you use this keyword, only the length of the last dimension can be changed. If we try to change other sizes or change the number of sizes, an error is displayed below.

alt= Execution error ‘9’ : Subscription out of range data-original width=549 data-original height=307 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357842_554_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />.

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Demonstration with the save keyword

Sub redim_pres_demo()

Step 1 : Declare array
Dim arr_furni() Variant

Step 2 : Define size and dimensions with the renamed keyword
ReDim arr_furni(4, 2) as variant

Step three: Initializing values

arr_furni(0, 0) = table
arr_furni(0, 1) = $5
arr_furni(0, 2) = 4 kg

arr_furni(1, 0) = chairman
arr_furni(1, 1) = $6
arr_furni(1, 2) = 3 kg

arr_furni(2, 0) = set banks
arr_furni(2, 1) = $170
arr_furni(2, 2) = 15 kg

arr_furni(3, 0) = double bed
arr_furni(3, 1) = $58
arr_furni(3, 2) = 34 kg

arr_furni(4, 0) = light seat
arr_furni(4, 1) = $9
arr_furni(4, 2) = 4 kg

Step 4 : Print all contents before printing the table
Debug.Print Step 4
For i = LBound(arr_furni) To UBound(arr_furni)
Debug.Print arr_furni(i, 0) & ; & arr_furni(i, 1) & ; & arr_furni(i, 2);
Debug.Print End of record
i next

Step 5 : Alternatively, try reducing the last dimension with the Preserve
ReDim Preserve arr_furni(4, 3) keyword.

Step 6 : Print all contents after resizing the table and changing the length of the last dimension
‘the contents must not be deleted
Debug.Print step 6
For i = LBound(arr_furni) To UBound(arr_furni)
Debug.Print arr_furni(i, 0) & ; & arr_furni(i, 1) & ; & arr_furni(i, 2) ;
Debug.Print end of next record
i

Last Subsection

The code is displayed:

Level 4

Table; $5; 4 kg

End of registration

Chair; $6; 3 kg

End of registration

Pillow; $170; 15 kg

End of registration

Double bed; $58; 34 kg

End of registration

Lightweight chair; $9; 4 kg

End of registration

Step 6

Table; $5; 4 kg

End of registration

Chair; $6; 3 kg

End of registration

Pillow; $170; 15 kg

End of registration

Double bed; $58; 34 kg

End of registration

Lightweight chair; $9; 4 kg

End of registration

alt=Write output on keyword width-original=960 data-original-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1611357843_505_How-Do-You-Redim-a-Multidimensional-Array.png data-ez= />.

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook-ezsrc=/utilcave_com/social/fb_share.png />

Completion

Paintings are really interesting objects to work with.

Although the keyword Stores the contents of the table, it has certain limitations. If there are cases where we absolutely need to adjust the first dimension instead of the second, we can try moving the painting, then resizing it, and moving it again to bring it back to its old structure.

redim preserveredim preserve 2d arrayvba redim preserve 2 dimensional array subscript out of rangevba redim array already dimensionedvba declare multidimensional array unknown sizevba ubound multidimensional arraymultidimensional array vbavba 2 dimensional array initialization

You May Also Like

How To Fix Thumbnails Not Showing In Windows 10

The Windows thumbnail function makes it easy to recognize photos and movies…

A Complete Guide On Resolving The Issue of Phone Overheating

The phone can sometimes even get hot, whether it’s during a game…

How To Fix Windows 10 File Explorer Dark Theme Not Working

When it comes to customisation options, Windows 10 offers users a considerable…

Apex Legends Crashing PC Fix 2020

Various problems can occur during gambling. If you’re playing the new royal…