Recording Data when using =rand()

advaddi

New Member
Joined
Jul 31, 2018
Messages
6
Hi all!,

Quick question: So im trying to create a financial model that uses the rand() and randbetween() functions to put in randomised growth rates for certain parameters for a company. Ive successfully set up the functions and output but i want to quick record the output each time I press F9 to randomize the growth rates. Is there a way I can save each F9 output without having to copy paste it each time?
Alternatively, i can duplicate the model across many excel sheet tabs and collect the outputs in a single sheet so it reduces the number of copy pastes required but a more efficient way would be much preferred.

Many thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The following VBA will do this for you
Code:
Option Explicit

Sub logResults()


' change these values as required:
Const iIterations As Integer = 100
Dim rngResult: Set rngResult = Sheet1.Range("A1")


' create dynamic array for holding results in memory
Dim arrResults(0 To iIterations, 1 To 2)
arrResults(0, 1) = "iteration"
arrResults(0, 2) = "result"


' recalculate model and store results
Dim iLoop As Integer
For iLoop = 1 To iIterations
    
    Application.Calculate


    arrResults(iLoop, 1) = iLoop
    arrResults(iLoop, 2) = rngResult.Value
    
Next iLoop


' create a new file and pass results to it
Dim wb As Workbook: Set wb = Workbooks.Add
wb.Sheets(1).Cells(1, 1).Resize(UBound(arrResults, 1) - LBound(arrResults, 1) + 1, UBound(arrResults, 2) - LBound(arrResults, 2) + 1).Value = arrResults


End Sub
 
Upvote 0
Clarification: I've referred to "Sheet1.Range("")", this is the name of the worksheet when viewed in the VB Editor window. It may or may not be the same as the name of the worksheet when viewed in Excel

You could also use Sheets("Sheet1").Range("") which creates a similar object based on the Excel name, but this is susceptible to errors when the worksheet is renamed

When you view the Project Explorer window in the VB Editor you'll see 2 names for each worksheet, one is the VBA Codename and one is the Excel name, hopefully this will make sense when you see it
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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