Home » excel » excel – Run-time error 9: Index out of range when accessing a simple array

excel – Run-time error 9: Index out of range when accessing a simple array

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve run into a problem, I cannot seem to figure out, using basic array functionalities. I’m using the following (standard) code:

Sub CC()

Dim arrB As Variant
arrB = Sheet2.Range("B3:B100").Value2

Debug.Print LBound(arrB) & " " & UBound(arrB)
Debug.Print arrB(LBound(arrB))

End Sub

The first Debug.Print statement returns “1 98” as expected. But when I try to access any values inside the array arrB with my second Debug.Print I’m getting a run-time error 9 (Index out of range). Any ideas on what I’m missing here?

How to&Answers:

Try:

Debug.Print arrB(LBound(arrB),1)

For future tasks with arrays – that was quick because I had many many struggles before, the easiest way to deal with arrays is to Add Watch on them.

By that You would see how it looks and how many dimensions it has 🙂

I’m glad that I could help 🙂