I’m working on a parser for use at work. Basically I have an excel document full of rows of data that I need to parse through and store into a multidimensional array to work with.
Here is the code I have so far:
Dim a
Dim iRows As Long
Dim i As Integer
Dim aArray() As Variant
a = Array(2,3,4,5,6,9,10,14,19,21,23,25,29,38)
iRows = ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
ReDim aArray(iRows, UBound(a))
For i = 2 To iRows + 1
For x = 0 To UBound(a)
aArray(i - 2, x) = ActiveSheet.Cells(i, a(i - 2)).Value
Next x
Next i
Keep in mind the offset of i = 2 and iRows + 1 is to ignore the header row
The problem is that when I attempt to output it I am finding that all the rows are the same so aArray(0,1) and aArray(1,1) are the same values which they should be different values from different rows of the excel file.
furthermore there should be 14 columns that I am storing per row but when i try to output any value past location 9 i get an out of range error like so
ActiveSheet.Cells(1, 1).Value = aArray(10, 0)
ActiveSheet.Cells(1, 2).Value = aArray(11, 0)
Based on the small array I am using to specify which values to store in the main array their should be 14 total locations for each sub array contain within aArray.
Any insights?
Keep in mind that this is my first real VBA script so if I am making mistakes please have patients with my I m coming from a JS/PHP background so this is a lot different for me.
You switched the dimensions in aArray. In the code you redim and fill aArray with (count_of_rows_in_usedRang,count_of_elements_in_a) and I imagine used rang is just 9 lines, so aArray(10, 0) is out of range while aArray(0,10) wouldn’t be.
I think there may be a similar problem with the output in the first part of your question. If not: please post, what is in the excel-sheet and what you got as a result.