Array question

Shiro26

Board Regular
Joined
Oct 2, 2015
Messages
82
Hi,

I have a simple question regarding arrays.
According to the MSDN help, the first element of a 2D array is located at (0,0) MSDN Link
https://msdn.microsoft.com/en-us/library/02e7z943.aspx

But, in my code, if I do the following:
Code:
Sub test2()

'Table with Header-A in Cell(10,10)
'
'Header-A    Header-B    Header-C    Header-D
'11          12          13          14
'21          22          23          24
'31          32          33          34
'41          42          43          44


Dim rng As Range, rngout As Range
Dim MyArr As Variant
Dim MyArrP As Variant 'I want to have at the begining an Array of 2 column 1 row to start
Dim MyArrPsize As Integer

ReDim MyArrP(1, 2)

Set rng = Range(Cells(11, 10), Cells(14, 13))
MyArr = rng

Set rngout = Range(Cells(20, 10), Cells(24, 13))

MyArrP(0, 0) = 9900
MyArrP(0, 1) = 9901
MyArrP(0, 2) = 9902
MyArrP(1, 0) = 9910
MyArrP(1, 1) = 9911
MyArrP(1, 2) = 9912
'MyArrP(2, 0) = 9920 '<= error 9
'MyArrP(2, 1) = 9921 '<= error 9
'MyArrP(2, 2) = 9922 '<= error 9
rngout = MyArrP


'MsgBox MyArr(0, 0)          '<== why is it returning an error? shouldn t it return 11

MyArrsize = UBound(MyArr, 1) - LBound(MyArr, 1) + 1
MyArrPsize = UBound(MyArrP, 1) - LBound(MyArrP, 1) + 1

MsgBox "MyArrSize = " & MyArrsize & vbNewLine & "LBound(MyArr, 1) = " & LBound(MyArr, 1) & vbNewLine & "UBound(MyArr, 1) = " & UBound(MyArr, 1) & vbNewLine & vbNewLine & "MyArrPSize = " & MyArrPsize & vbNewLine & "LBound(MyArrP, 1) = " & LBound(MyArrP, 1) & vbNewLine & "UBound(MyArrP, 1) = " & UBound(MyArrP, 1)

ReDim Preserve MyArrP(MyArrPsize + 1, 2)                      'I want to redim and preserve the value and extend the MyArrP to 2 columns and 2 rows but get error 9

End Sub


1) Why is MyArr(0,0) is not returning 11 when putting a range into an array?
2) Why is MyArrP(0,0) returning 9900 unlike question 1?
3) How can I avoid the error 9 when redimensioning and preserving the value of MyArrP?

Thank you in advance for your insights because I am getting lost in the array usage...

Shiro
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Shiro

Your array is variant. Since you have assigned the range to the array , the dimension of the array will be dimension of range.

For example,

Code:
Dim ExampleArr()     As Variant
ExampleArr = Range("B1:B20")

VBA Automatically assigns dimensions as ExampleArr (1 to 20, 1 To 1) as per the range.
 
Last edited:
Upvote 0
I think that you are misreading the microsoft link.

The first element of a 2D array depends on how the array is dimensioned.
The default lowest index is 0, unless changed by an Option Base 1 statement, in which case it is 1
compare these:
Code:
Sub mySub
    Dim myArray(2, 3)

   MsgBox LBound(myArray, 1)
End Sub

Code:
Option Base 1

Sub mySub
    Dim myArray(2, 3)

   MsgBox LBound(myArray, 1)
End Sub
which is why it I consider it good practice to explicitly set the lower bound when dimensioning an array, to exactly what you want.
Code:
Sub mySub3()
    Dim myArray(1 to 4, -1 to 8)

    MsgBox LBound(myArray, 1)
End Sub

Notice that you can set the bounds of the indicies to any values that you want, including negative values.

In the OP sub, myArr gets its dimension and values from the statement

Code:
MyArr = rng.Value

When an array gets its value by setting it to the .VAlue of an array, it is dimensioned just line the range would be, a 1 based result with two dimensions.
 
Upvote 0
Hello Both of you,

Thank you for your reply.

I have never used Option Base 1, hence I was not familiar with it.
As both of you suggested, and although I would like to dimension the array from the start, unfortunately, I cannot...
because when I tried to I was not able to redimension it later on
and also because total number of value I will store in that array is variable, hence I wanted to kept it to the minimum.

More detailed, here is what I am trying to do:
> build a 2 column 1 row empty array
> intitialise by putting a value as in Array stage 1 AND resize my array by adding one row
> continue to populate while having always one blank row empty (noted xx in the below table)
> sometimes, the lowest value is decreased and , when that value becomes 0 -such as in 6a- then I want to get rid of that line so that the size of my array is always the minimum necessary -such as 6b-.
> and so on..


[TABLE="class: grid, width: 799"]
<tbody>[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]4[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]empty[/TD]
[TD="align: right"]empty[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] xx[/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]6a[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]6b[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"]Array stage[/TD]
[TD="align: right"]8[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]8[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"][/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]50[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD="align: right"] [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"][/TD]
[TD="align: right"]xx [/TD]
[TD="align: right"]xx [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]


I understand from your explanations that I can use Option Base 1 to have (1,1).
But, how can I resize my array so that I can fulfill the action as described above?
I have tried several small examples and always got the same 9 error...

Please do not hesitate if I was not clear enough.

Thank you

Shiro
 
Last edited:
Upvote 0
Actualy, you cannot ReDim an array row variable. Redim Preserve only works on the last dimension of a 2D array.
 
Upvote 0
Hi Mike,

Thank you for the time you take replying again...

So basically, it leaves me only two options:
1- either I create a temporary array that I use as a buffer to redim without preserving and copy paste from one table to the other one the "preservation"
I think I can copy paste from one array to the other one but it increases the calculation time...

2- either I create a table big enough to swallow but in that case I have to retrieve everytime the smallest 1st column for which the 2 column value is not 0...
I don t know how to perform such lookup -search for such value in an array in vba...


Do you see another alternative possible from your experience?

Thank you in advance.

Kind Regards,

Shiro
 
Upvote 0
You could construct you array to work on the transpose. And use Application.Transpose after you are done.
Code:
Dim myArray(1 to 2, 1 to 1)

myArray(1,1) = 1
myArray(2, 1) = 1
Redim Preserve myArray(1 to 2, 1 to 2)

myArray(1,2) = 2
myArray(2, 2) = 20
Redim Preserve myArray(1 to 2, 1 to 3)


Or you could initially dimensions the array very large and use a pointer to indicate where you are working

Code:
Dim Pointer as Long: Pointer = 0
Dim myArray(1 to 10000, 1 To 2)

Pointer = Pointer + 1
myArray(Pointer, 1) = 1 : myArray(Pointer, 2) = 10

Pointer = Pointer + 1
myArray(Pointer, 1) = 2 : myArray(Pointer, 2) = 20
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top