ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   subscript out of range (https://www.excelbanter.com/excel-programming/344148-subscript-out-range.html)

lvcha.gouqizi

subscript out of range
 
I define a type

Type Change

nextChanges() As Integer
nextNum As Integer
prevChanges() As Integer
prevNum As Integer
End Type


and an array of type Change
Dim myChanges() as Change

when I fill in data to myChanges like
myChanges(index).nextChanges(subIndex) = ***

It always says "subscript out range". I checked that the index is 1
and subIndex is 10. What't the reason for this?

thank.


Jim Thomlinson[_4_]

subscript out of range
 
This worked for me in a regular module...

Type Change
nextChanges() As Integer
nextNum As Integer
prevChanges() As Integer
prevNum As Integer
End Type

Sub test()
Dim myChanges() As Change

ReDim myChanges(5)
ReDim myChanges(1).nextChanges(5)

myChanges(1).nextChanges(1) = 1
myChanges(1).nextChanges(2) = 2
myChanges(1).nextChanges(3) = 3
myChanges(1).nextChanges(4) = 4

MsgBox myChanges(1).nextChanges(1)
MsgBox myChanges(1).nextChanges(2)
MsgBox myChanges(1).nextChanges(3)
MsgBox myChanges(1).nextChanges(4)

End Sub

You will have to post more code if you need more help...
--
HTH...

Jim Thomlinson


"lvcha.gouqizi" wrote:

I define a type

Type Change

nextChanges() As Integer
nextNum As Integer
prevChanges() As Integer
prevNum As Integer
End Type


and an array of type Change
Dim myChanges() as Change

when I fill in data to myChanges like
myChanges(index).nextChanges(subIndex) = ***

It always says "subscript out range". I checked that the index is 1
and subIndex is 10. What't the reason for this?

thank.



Leith Ross[_145_]

subscript out of range
 

Hello lvcha.gouqizi,

When using dynamic arrays, arrays with empty (), you must use the ReDi
statement to set the element size of the array. If you have not set th
size of the array, the system will give you subscript out of range.

Your UDT (User Defined Type) contains 2 dynamic arrays which must b
set to some size before you try to store a value to them. ReDim allow
you to change the number of elements as needed, but you must change th
array size before trying to store or retrieve a value to avoid th
subscript out of range error.

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=48025


Jim Thomlinson[_4_]

subscript out of range
 
You will also get this error if you try to fill the 11th item of a 10 item
array. The array may be initalized but if it is too small then you are
hooped. Unlike C which will allow you to write outside the boundaries of the
array which is a horror I am glad to be done with.
--
HTH...

Jim Thomlinson


"Leith Ross" wrote:


Hello lvcha.gouqizi,

When using dynamic arrays, arrays with empty (), you must use the ReDim
statement to set the element size of the array. If you have not set the
size of the array, the system will give you subscript out of range.

Your UDT (User Defined Type) contains 2 dynamic arrays which must be
set to some size before you try to store a value to them. ReDim allows
you to change the number of elements as needed, but you must change the
array size before trying to store or retrieve a value to avoid the
subscript out of range error.

Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=480258



lvcha.gouqizi

subscript out of range
 
Thanks all.

So what's the use of Redim Preserve?

I still get "subscript out of range" error when I modified my code to
the below

Type Change
nextChanges() As Integer
nextNum As Integer
prevChanges() As Integer
prevNum As Integer
End Type

Dim myChanges() as Change

Redim myChanges(20)
Dim i as integer
for i = 1 to 20
Redim myChanges(i).nextChanges(50)Type Change
Redim myChanges(i).prevChanges(50)Type Change
next i


.......
myChanges(index).nextChanges(subIndex) = ***
Redim preserve myChanges(index).nextChanges(subIndex)


The error is reported in the Redim preserve line. What's wrong with it?


Leith Ross[_170_]

subscript out of range
 

Hello lvcha.gouqizi,


Dim myChanges() as Change

ReDim myChanges(20)
ReDim nextChanges(50)
ReDim prevChanges(50)

Dim i as integer

for i = 1 to 20
myChanges(i).nextChanges(50)Type Change
myChanges(i).prevChanges(50)Type Change
next i

In this line where do you define and assign the values to Index and
SubIndex
myChanges(index).nextChanges(subIndex) = ***

This line saves the changes to the Array's data held the last
dimension. Redim only affects the last dimension of an array. Other
indexes remain unchanged.
Redim preserve myChanges(index).nextChanges(subIndex)

Sincerely,
Leith Ross


--
Leith Ross


------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=480258



All times are GMT +1. The time now is 11:34 PM.

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