I'm looking at populating an array based off columns in an original Excel table. I'm attempting to loop through to iteratively obtain each combination of fields, in order to populate into second Excel table subsequently. Thus far i have populated 5 individual arrays, and obtained the count of the data in them, however its when i attempt to populate the 'calcarray' where i'm encountering issues. When executing I'm getting run-time error '9' subscript out of range on "calcarray(x, 4) = Data5(d)" Any assistance would be appreciated!
Sub populate_table()
Dim Data1() As Variant
Dim Data2() As Variant
Dim Data3() As Variant
Dim Data4() As Variant
Dim Data5() As Variant
Dim Data1Count As Integer
Dim Data2count As Integer
Dim Data3Count As Integer
Dim Data4Count As Integer
Dim Data5Count As Integer
Dim ttl As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim f As Integer
Dim d As Integer
Dim tbl As ListObject
Set tbl = Sheets("Data").ListObjects("tbl_variables")
Data1Count = tbl.ListColumns("Data1").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Count
Data2count = tbl.ListColumns("Data2").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Count
Data3Count = tbl.ListColumns("Data3").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Count
Data4Count = tbl.ListColumns("Data4").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Count
Data5Count = tbl.ListColumns("Data5").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Count
Data1 = Array(tbl.ListColumns("Data1").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Value)
Data2 = Array(tbl.ListColumns("Data2").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Value)
Data3 = Array(tbl.ListColumns("Data3").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Value)
Data4 = Array(tbl.ListColumns("Data4").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Value)
Data5 = Array(tbl.ListColumns("Data5").DataBodyRange.Rows.SpecialCells(xlCellTypeConstants).Value)
ttl = (Data1Count) * (Data2count) * (Data3Count) * (Data4Count) * (Data5Count)
Dim calcarray() As Variant
ReDim calcarray(ttl, 4)
x = 0
For i = 0 To Data1Count
For j = 0 To Data2count
For k = 0 To Data3Count
For f = 0 To Data4Count
For d = 0 To Data5Count
calcarray(x, 0) = Data1(i)
calcarray(x, 1) = Data2(j)
calcarray(x, 2) = Data3(k)
calcarray(x, 3) = Data4(f)
calcarray(x, 4) = Data5(d)
x = x + 1
Next
Next
Next
Next
Next
ReDim calcarray(ttl,5)is the same asReDim calcarray(0 to ttl, 0 to 5)unless you're usingOption Base 1(and don't use that!)Valueproperty of a multi-area range, and if you could then you can't then wrap it inArray()to get an array you can loop over via indexing in aFor...Nextloop. If you put a watch on your arrays you will see they're likely not as you expect.