ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Array range error question (https://www.excelbanter.com/excel-programming/440973-array-range-error-question.html)

miek

Array range error question
 
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

Array range error question
 
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


All times are GMT +1. The time now is 04:38 AM.

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