Generate 1000 Iterations of a Calculated Value

rwmill9716

Active Member
Joined
May 20, 2006
Messages
495
Office Version
  1. 2013
Platform
  1. Windows
I have a program that randomly samples a population, then generates a value from that sampling. As shown in the image (and the table accessible from the web address included), columns E, G, I, K, and M use the randomly sampling function shown in row 4 to randomly sample data in column B. Five summary statistics are provided in row 6 and copied to columns P, Q, R, S, and T. When I hit F9 all, except column B data, change. I need a macro that hits F9 1,000 times then accumulates data for each iteration in columns P, Q, R, S, and T so finally, I'll have these data in rows 1 to 1,000.

 

Attachments

  • Test Random.jpg
    Test Random.jpg
    147.2 KB · Views: 32

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try:

VBA Code:
Sub Test()

    Dim i As Long, j As Long, N As Long, k As Long
    Dim vResults As Variant, vResultsAll() As Double
    
    N = 1000
    k = 5
    ReDim vResultsAll(1 To N, 1 To k)
    Application.ScreenUpdating = False
    
    For i = 1 To N
        Calculate
        vResults = Range("F6:N6").Value
        For j = 1 To k
            vResultsAll(i, j) = vResults(1, j * 2 - 1)
        Next j
    Next i
    
    Range("P1").Resize(N, k).Value = vResultsAll
    Application.ScreenUpdating = True
    MsgBox "Finished"

End Sub
 
Upvote 0
Solution
Try:

VBA Code:
Sub Test()

    Dim i As Long, j As Long, N As Long, k As Long
    Dim vResults As Variant, vResultsAll() As Double
   
    N = 1000
    k = 5
    ReDim vResultsAll(1 To N, 1 To k)
    Application.ScreenUpdating = False
   
    For i = 1 To N
        Calculate
        vResults = Range("F6:N6").Value
        For j = 1 To k
            vResultsAll(i, j) = vResults(1, j * 2 - 1)
        Next j
    Next i
   
    Range("P1").Resize(N, k).Value = vResultsAll
    Application.ScreenUpdating = True
    MsgBox "Finished"

End Sub
Stephen,
I get the attached error message when I run your macro. Thanks for working on this.
Ric
 

Attachments

  • Macro.jpg
    Macro.jpg
    28 KB · Views: 14
Upvote 0
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top