Subscript Out Of Range 
Author Message
 Subscript Out Of Range

I have some sample code here which is similar to what I saw in a book.  The
only diference is that I'm using a range of cells from Excel and the book
used a recordset to populate the array.  I'm getting the err msg:
"Subscript Out Of Range" on the 9th line:
  "Debug.Print varA(iRow, iCol);"

when this code loops through the first time, both iRow and iCol equal "0".
Thats where it errors.  If I set the begining value for iRow and iCol to "1"
then it runs OK unitil the last loop where iCol  is one integer too high.

Please advise on the proper way to loop through an array like this.

Thanks.

Private Sub MakeRangeArray()
'On Error GoTo err_TestErr
    Dim varA As Variant
    Dim iCol As Integer
    Dim iRow As Integer
    varA = Worksheets("Sheet3").Range("C5:I12")

    For iRow = 0 To UBound(varA, 2)
        For iCol = 0 To UBound(varA, 1)
            Debug.Print varA(iRow, iCol);
        Next iCol
        Debug.Print
    Next iRow

err_TestErr:
    If Err.Number = 9 Then Resume Next
End Sub

--
George Padvorac

nwis.net



Tue, 15 May 2001 03:00:00 GMT  
 Subscript Out Of Range
Hi George,

The error should occur here as in Excel, the lower bound of all cells and
ranges is 1, not 0.  You might also want to reference the Cells themselves
directly instead of using the Range.

HTH
--
Dev Ashish (Just my $.001)
---------------
The Access Web ( http://home.att.net/~dashish )
---------------

Quote:

>I have some sample code here which is similar to what I saw in a book.  The
>only diference is that I'm using a range of cells from Excel and the book
>used a recordset to populate the array.  I'm getting the err msg:
>"Subscript Out Of Range" on the 9th line:
>  "Debug.Print varA(iRow, iCol);"

>when this code loops through the first time, both iRow and iCol equal "0".
>Thats where it errors.  If I set the begining value for iRow and iCol to
"1"
>then it runs OK unitil the last loop where iCol  is one integer too high.

>Please advise on the proper way to loop through an array like this.

>Thanks.

>Private Sub MakeRangeArray()
>'On Error GoTo err_TestErr
>    Dim varA As Variant
>    Dim iCol As Integer
>    Dim iRow As Integer
>    varA = Worksheets("Sheet3").Range("C5:I12")

>    For iRow = 0 To UBound(varA, 2)
>        For iCol = 0 To UBound(varA, 1)
>            Debug.Print varA(iRow, iCol);
>        Next iCol
>        Debug.Print
>    Next iRow

>err_TestErr:
>    If Err.Number = 9 Then Resume Next
>End Sub

>--
>George Padvorac

>nwis.net



Thu, 17 May 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Subscript out of range error

2. Subscript out of range error

3. Subscript out of range error

4. Subscript out of range

5. AddShapeRecordDetected Error: Subscript out of range

6. Defining an array and getting Subscript Out of Range Error

7. array subscript out of range

8. help with subscript out of range error

9. subscript out of range? (floating array problem)

10. Xarray ended up with Subscript out of range in Asian locale

11. Visual Basic - Subscript Out Of Range

12. Problems With FlexGrid And Subscript Out Of Range Error

 

 
Powered by phpBB® Forum Software