After starting the simulation with 100,000 iterations, I tried to derive the values โโfrom each iteration into a column. Here is the gist of the code:
Sub test() Application.ScreenUpdating = False Dim totalgoals() As Variant, ko As Worksheet, out As Worksheet, iter As Long Set ko = Sheets("KO Sim") Set out = Sheets("Monte Carlo") iter = out.Range("P2").Value For i = 1 To iter ko.Calculate If i = 1 Then ReDim totalgoals(1 To 1, 1 To 1) As Variant totalgoals(1, 1) = ko.Range("F23").Value Else ReDim Preserve totalgoals(1 To 1, 1 To i) As Variant totalgoals(1, i) = ko.Range("F23").Value End If Next i out.Range("U1:U" & iter) = Application.WorksheetFunction.Transpose(totalgoals) Application.ScreenUpdating = True End Sub
This causes a type mismatch error on the next line, because Transpose can handle arrays up to 2 ^ 16 (~ 64,000) long. So how do I solve this? What is my most effective option?
I set my code to store values โโin an array just for simple output, but it doesn't seem to work for these many values. Will it be better to stick with arrays and just write my own transpose function (i.e., iterate over the array and write the values โโto a new array), or will I be better off working with another class from the very beginning, for example, with a collection, if I still have to punching results all the time?
Or even better, is there anyway to do this without iterating over the values โโagain?
EDIT:
I presented a bad example, because ReDim Preserve calls are not needed. So, consider the following, where they are needed.
ReDim totalgoals(1 To 1, 1 To 1) As Variant For i = 1 To iter ko.Calculate If ko.Range("F23") > 100 Then If totalgoals(1, 1) = Empty Then totalgoals(1, 1) = ko.Range("F23").Value Else ReDim Preserve totalgoals(1 To 1, 1 To UBound(totalgoals, 2) + 1) As Variant totalgoals(1, UBound(totalgoals, 2)) = ko.Range("F23").Value End If End If Next i out.Range("U1").Resize(UBound(totalgoals, 2),1) = Application.WorksheetFunction.Transpose(totalgoals)