Super User Asked by JeRunEnChaussettes on November 9, 2021
So I primarily use java and C++ and VBA is very new to me. I’m very confused as to why I keep getting error 9 – "Index out of range". Here is the code:
Dim count As Integer
Dim j As Integer
count = months.count
Dim monthsArr() As String
ReDim monthsArr(1, count)
For j = 1 To count
monthsArr(j) = months.Item(j)
Next j
Count is equal to 10 (I checked with a msgBox) and so months is an ArrayList of length 10. I’m trying to put the values of months into an array so I can use that array on an autofilter. I keep getting an error at this line:
monthsArr(j) = months.Item(j)
I thought VBA used 1-indexing for collections but I really don’t know. Anybody know why I keep getting this error? Thanks
You have re-dimensioned monthsArr
to be a 2-dimension array ReDim monthsArr(1, count)
.
If it should remains a 2-dimension array, then when you assign elements to it, you must also show which part of the array on both dimension. You are only using one: monthsArr(j) = months.Item(j)
. It should be something like monthsArr(j,i) = months.Item(j)
where i
is the second dimension of the array.
Also, note that 0 is the first in an array, not 1. So when you declare monthsArr(1,count)
, it means that the first dimension has 2 elements/items while the second dimension will have count + 1 elements.
If it is not meant to be a 2-diension array but a 1-dimension array, then you should use: ReDim monthsArr(count)
or ReDim monthsArr(count-1)
as the its 0-based.
Answered by Reddy Lutonadio on November 9, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP