Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default 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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default 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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default 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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default 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?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subscript out of Range Steve Excel Discussion (Misc queries) 3 April 15th 09 04:01 PM
Subscript Out of Range ExcelMonkey[_190_] Excel Programming 6 February 20th 05 02:46 AM
Subscript out of range.... Edgar Thoemmes[_4_] Excel Programming 4 January 13th 05 02:06 AM
Subscript is out of Range teresa Excel Programming 2 December 19th 04 05:00 PM
subscript out of range Todd Huttenstine[_3_] Excel Programming 1 June 11th 04 04:08 AM


All times are GMT +1. The time now is 03:18 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"