joejackson123
New Member
- Joined
- Oct 27, 2017
- Messages
- 16
Hi All, I have the following VBA script, which works for doing a series of somewhat random draws from a model, stores those values in an array, and then pastes the array into a sheet for graphing. Storing values from a single cell on each draw works fine, when I try to store an entire range of cells in the
array variable, it throws an error. I'm 90% certain this has something to do with sizing/resizing of the array, but am fairly green with VBA and cribbed most of this code from online, so am not entirely sure. Any help is as always much appreciated, thank you!
Code:
curvearray
Code:
[FONT=arial]Dim randa As Double[/FONT]
[FONT=arial]Dim stdeva As Double[/FONT]
[FONT=arial]Dim looprun As Double[/FONT]
[FONT=arial]Dim arrayone() As Variant[/FONT]
[FONT=arial]Dim arraytwo() As Variant[/FONT]
[FONT=arial]Dim curvearray() As Variant[/FONT]
[FONT=arial]ReDim [/FONT][FONT=arial]arrayone[/FONT][FONT=arial](1 To 1)[/FONT]
[FONT=arial]ReDim [/FONT][FONT=arial]arraytwo[/FONT][FONT=arial] [/FONT][FONT=arial](1 To 1)[/FONT]
[FONT=arial]ReDim curvarray(1 To 10)[/FONT]
[FONT=arial]stdeva = 1[/FONT]
[FONT=arial]a = Range("baseline").Value[/FONT]
[FONT=arial]For looprun = 1 To 100[/FONT]
[FONT=arial]randa = Rnd[/FONT]
[FONT=arial]Range("baseline").Value = WorksheetFunction.NormInv([/FONT]<wbr style="font-family: arial, sans-serif; font-size: small;">[FONT=arial]randa, a, stdeva)[/FONT]
[FONT=arial]Application.Calculate[/FONT]
[FONT=arial]arrayone[/FONT][FONT=arial] [/FONT][FONT=arial](UBound(arrayone)) = Range("net").Value[/FONT]
[FONT=arial]ReDim Preserve [/FONT][FONT=arial]arrayone[/FONT][FONT=arial](1 To UBound([/FONT][FONT=arial] [/FONT][FONT=arial]arrayone[/FONT][FONT=arial]) + 1)[/FONT]
[FONT=arial]arraytwo(UBound([/FONT][FONT=arial]arraytwo[/FONT][FONT=arial])) = Range("multi").Value[/FONT]
[FONT=arial]ReDim Preserve [/FONT][FONT=arial]arraytwo[/FONT][FONT=arial](1 To UBound([/FONT][FONT=arial]arraytwo[/FONT][FONT=arial]) + 1)[/FONT]
[FONT=arial]curvearray(UBound([/FONT][FONT=arial]curvearray)) = Range("curve").Value[/FONT]
[FONT=arial]ReDim Preserve curvearray(1 To UBound(curvearray) + 1)[/FONT]
[FONT=arial]Next looprun[/FONT]
[FONT=arial]Range("onepaste").Resize([/FONT]<wbr style="font-family: arial, sans-serif; font-size: small;">[FONT=arial]UBound(arrayone)) = Application.Transpose([/FONT][FONT=arial]arrayone[/FONT][FONT=arial])[/FONT]
[FONT=arial]Range("twopaste").Resize([/FONT]<wbr style="font-family: arial, sans-serif; font-size: small;">[FONT=arial]UBound([/FONT][FONT=arial]arraytwo[/FONT][FONT=arial])) = Application.Transpose([/FONT][FONT=arial]arraytwo[/FONT][FONT=arial])[/FONT]
[FONT=arial]Range("curvepast").Resize([/FONT]<wbr style="font-family: arial, sans-serif; font-size: small;">[FONT=arial]UBound([/FONT][FONT=arial]curvearray[/FONT][FONT=arial])) = Application.Transpose([/FONT][FONT=arial]curvearray[/FONT][FONT=arial])[/FONT][FONT=arial] [/FONT]
[FONT=arial]End Sub[/FONT]