Hi all,
I've written a very basic macro (the only kind I know how!) which simulates the rolling of a die for a specified number of trials, recording each result as a hard-coded value in a table. It works fine but it is very slow. 1000 trials takes around 1.5 minutes. Yet I've seen other simulations in Excel go several times faster. Is there anything I can do to speed this up?
The code is below.
Basically, the user types in the number of trials in a cell named "Number_of_trials" and then runs the macro. Then, for each trial,
-- the die roll is simulated with a formula which uses the RAND function. This formula is in a cell named "ResultsLive". For each trial, the worksheet recalculates so that RAND generates a new value here.
-- the macro goes to this "ResultsLive" cell, copies the result, then goes to the top of the results table -- which is a cell named "FirstResults" -- and pastes it as a hard-coded value.
It then repeats as neccessary, storing each new result below the previous one, until the loop ends.
#######################################################
##################Code begins###########################
''' For the specified number of times, generates a random decimal between 0 and 1
''' and records it in a list
Dim i As Integer
For i = 0 To Range("Number_of_trials").Value
Calculate
Application.Goto Reference:="ResultsLive"
Selection.Copy
Application.Goto Reference:="FirstResults"
ActiveCell.Offset(i, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Next i
##################Code ends############################
#######################################################
My guess is that it's slow because the macro is literally "travelling" here and there many times as result of my use of "Goto Reference". Is there a quicker way to get each new, recalculated value to the proper destination cell?