PC Review


Reply
Thread Tools Rate Thread

Array range error question

 
 
miek
Guest
Posts: n/a
 
      24th Mar 2010
When i try to redim my array i get an out of range error. can someone tel me
why?
Cells A1..A10 are filled with strings. thanks much

Sub Get_names_array()
Dim names_array() As Variant 'dynmanic
Dim j As Long

ReDim names_array(5, 1)

j = 0
Range("A1").Select
For i = 1 To 10
names_array(j, 0) = ActiveCell.Value
names_array(j, 1) = ActiveCell.Address
arraylimitFirst = UBound(names_array, 1) 'returns current dimension of 1st
array limit, init = 5
If j >= arraylimitFirst Then
ReDim Preserve names_array(10, 1) ' << RANGE ERROR 9 SUBSCRIPT OUT OF RANGE
j = j + 1
End If
ActiveCell.Offset(1, 0).Select 'down one
j = j + 1
Next i
End Sub

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      24th Mar 2010
Take a look at VBA's help for redim and you'll see this statement:

If you use the Preserve keyword, you can resize only the last array dimension
and you can't change the number of dimensions at all.

miek wrote:
>
> When i try to redim my array i get an out of range error. can someone tel me
> why?
> Cells A1..A10 are filled with strings. thanks much
>
> Sub Get_names_array()
> Dim names_array() As Variant 'dynmanic
> Dim j As Long
>
> ReDim names_array(5, 1)
>
> j = 0
> Range("A1").Select
> For i = 1 To 10
> names_array(j, 0) = ActiveCell.Value
> names_array(j, 1) = ActiveCell.Address
> arraylimitFirst = UBound(names_array, 1) 'returns current dimension of 1st
> array limit, init = 5
> If j >= arraylimitFirst Then
> ReDim Preserve names_array(10, 1) ' << RANGE ERROR 9 SUBSCRIPT OUT OF RANGE
> j = j + 1
> End If
> ActiveCell.Offset(1, 0).Select 'down one
> j = j + 1
> Next i
> End Sub


--

Dave Peterson
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
array, range question =?Utf-8?B?bXdhbTQyMw==?= Microsoft Excel Misc 4 23rd Aug 2007 01:36 AM
array from range question Gary Keramidas Microsoft Excel Programming 2 24th Apr 2007 07:26 PM
Question about working with range array 39N 95W Microsoft Excel Programming 2 18th Jan 2006 11:49 PM
Range and Array question aj Microsoft Excel Programming 0 21st Mar 2005 10:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:37 PM.