ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UBound array problem (https://www.excelbanter.com/excel-programming/428889-ubound-array-problem.html)

xavi garriga

UBound array problem
 
Dears;

I have a problem with a "For", the code is similar to this one:

for x = 1 to UBound(Array())
IF "some conditions" then
Redim Preserve Array(UBound(Arrray())-1)
x = x-1
end if
next x

For example supose the array [10,3].
The conditions make reference to the current array postion
for x = 1 supose the conditions are accomplished and the array would become:
[10]
and x will be x = 0.

Next x is x = 1. Now the conditions are not accomplised, so the array
continues being [10] and next x = 2.

The problem is that for X = 2, goes once more inside the loop but the
UBound of array is 1, so now we are out of range!
How can I solve this?

Thanks to all
--
atrep

Jacob Skaria

UBound array problem
 
Dear

Ubound by default returns first dimension or otherwise you need to specify
the dimension.

Dim myArray As Variant

ReDim myArray(10)
MsgBox UBound(myArray) 'will return 10

ReDim myArray(10, 5)
MsgBox UBound(myArray) ' will return 10
MsgBox UBound(myArray, 2) ' will return 2nd dimension

ReDim myArray(10, 5, 2)
MsgBox UBound(myArray) ' will return 10
MsgBox UBound(myArray, 3) ' will return 3nd dimension

Try the below code and and you will understand how to deal with that...Try
and feedback...


ReDim myArray(10, 0)

For x = 1 To 5
If x UBound(myArray, 2) Then
ReDim Preserve myArray(10, x)
'your code

'/your code
End If
Next




--
If this post helps click Yes
---------------
Jacob Skaria


"xavi garriga" wrote:

Dears;

I have a problem with a "For", the code is similar to this one:

for x = 1 to UBound(Array())
IF "some conditions" then
Redim Preserve Array(UBound(Arrray())-1)
x = x-1
end if
next x

For example supose the array [10,3].
The conditions make reference to the current array postion
for x = 1 supose the conditions are accomplished and the array would become:
[10]
and x will be x = 0.

Next x is x = 1. Now the conditions are not accomplised, so the array
continues being [10] and next x = 2.

The problem is that for X = 2, goes once more inside the loop but the
UBound of array is 1, so now we are out of range!
How can I solve this?

Thanks to all
--
atrep


xavi garriga

UBound array problem
 
Thanks!! It works perfectly!
--
atrep


"Jacob Skaria" wrote:

Dear

Ubound by default returns first dimension or otherwise you need to specify
the dimension.

Dim myArray As Variant

ReDim myArray(10)
MsgBox UBound(myArray) 'will return 10

ReDim myArray(10, 5)
MsgBox UBound(myArray) ' will return 10
MsgBox UBound(myArray, 2) ' will return 2nd dimension

ReDim myArray(10, 5, 2)
MsgBox UBound(myArray) ' will return 10
MsgBox UBound(myArray, 3) ' will return 3nd dimension

Try the below code and and you will understand how to deal with that...Try
and feedback...


ReDim myArray(10, 0)

For x = 1 To 5
If x UBound(myArray, 2) Then
ReDim Preserve myArray(10, x)
'your code

'/your code
End If
Next




--
If this post helps click Yes
---------------
Jacob Skaria


"xavi garriga" wrote:

Dears;

I have a problem with a "For", the code is similar to this one:

for x = 1 to UBound(Array())
IF "some conditions" then
Redim Preserve Array(UBound(Arrray())-1)
x = x-1
end if
next x

For example supose the array [10,3].
The conditions make reference to the current array postion
for x = 1 supose the conditions are accomplished and the array would become:
[10]
and x will be x = 0.

Next x is x = 1. Now the conditions are not accomplised, so the array
continues being [10] and next x = 2.

The problem is that for X = 2, goes once more inside the loop but the
UBound of array is 1, so now we are out of range!
How can I solve this?

Thanks to all
--
atrep



All times are GMT +1. The time now is 05:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com