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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,224,823
Messages
6,181,173
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