Macro to do repeated calculation..

traihan

New Member
Joined
Oct 9, 2023
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, have very little knowledge writing macros.

What I needed to do was:

Create 5 random numbers between (let's say) 39 and 46. Calculate %RSD of those 5 random numbers. Repeat this say 1000 times. Then find the highest %RSD from the 1000 RSDs. Hope someone can help. TIA>
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Here's a start:
VBA Code:
Function rsdCalc(VarA, VarB, runs)
Dim x() As Integer
Dim stdDevX As Long
Dim averageX As Long

ReDim x(1 To runs)

For i = 1 To runs

x(i) = WorksheetFunction.RandBetween(VarA, VarB)

Next

stdDevX = WorksheetFunction.StDev(x)
averageX = WorksheetFunction.Average(x)
rsdCalc = stdDevX / averageX * 100

End Function
 
Upvote 0
Maybe this will help:
VBA Code:
Function MultiRSD(LowerRnd As Long, UpperRnd As Long, RepeatFor As Long)
    Dim RndVar As Variant, rsd() As Variant, x As Long
   
    With Application
        ReDim rsd(RepeatFor - 1)
        For x = 0 To RepeatFor - 1
            RndVar = .RandArray(1, 5, LowerRnd, UpperRnd, True)
            rsd(x) = .StDev(RndVar) / .Average(RndVar) * 100
        Next x
        MultiRSD = .Max(rsd)
    End With
End Function

Used like:
Excel Formula:
=MultiRSD(39,46,1000)
Or
VBA Code:
Sub test()
    MsgBox MultiRSD(39, 46, 1000)
End Sub
 
Upvote 0
Solution
Open Excel
Press ALT+F11 (or navigate to the VBE)
Right click on 'ThisWorkbook'
Goto 'Insert' then 'Module'
Paste the MultiRSD code from above in the module you just created.

Now go back to Excel and in a cell type:
Excel Formula:
=MultiRSD(39,46,1000)

If you would rather get the result through a message box then:
In the module you created above, also paste the below:
VBA Code:
Sub test()
    MsgBox MultiRSD(39, 46, 1000)
End Sub

Then go back to Excel and press ALT+F8 then select the macro 'test' and click run
 
Upvote 0
Thank you it Worked. Gives me the largest RSD.

One thing I did not realize that it would not print out the random numbers associated with the highest RSD. One thing I also need to know are the possible set of numbers that produced the high RSDs. Is that doable? I understand it is going to be big data set.

Again appreciate all you help.
 
Upvote 0
Here's a start:
VBA Code:
Function rsdCalc(VarA, VarB, runs)
Dim x() As Integer
Dim stdDevX As Long
Dim averageX As Long

ReDim x(1 To runs)

For i = 1 To runs

x(i) = WorksheetFunction.RandBetween(VarA, VarB)

Next

stdDevX = WorksheetFunction.StDev(x)
averageX = WorksheetFunction.Average(x)
rsdCalc = stdDevX / averageX * 100

End Function
Hi bassplr19, can you please also guide me how I run this macro.
 
Upvote 0
Open Excel
Press ALT+F11 (or navigate to the VBE)
Right click on 'ThisWorkbook'
Goto 'Insert' then 'Module'
Paste the rsdCalc code from above in the module you just created.

Now go back to Excel and in a cell type:
Excel Formula:
=[B]rsdCalc[/B](39,46,1000)

Follow nearly the exact same procedure above
 
Upvote 0
One thing I did not realize that it would not print out the random numbers associated with the highest RSD. One thing I also need to know are the possible set of numbers that produced the high RSDs. Is that doable? I understand it is going to be big data set.

You can try the below, it will return 5 columns with the random numbers, column 6 will be the StDev result for that row, at the top of the StDev column will be the max value from all of the StDev's:

VBA Code:
Function MultiRSD(LowerRnd As Long, UpperRnd As Long, RepeatFor As Long)
    Dim RndVar As Variant, rsd() As Variant, x As Long, y As Long
  
    With Application
        ReDim rsd(RepeatFor, 5)
        For x = 1 To RepeatFor
            RndVar = .RandArray(1, 5, LowerRnd, UpperRnd, True)
            For y = 0 To 4
                rsd(x, y) = RndVar(y + 1)
            Next y
            rsd(x, y) = .StDev(RndVar) / .Average(RndVar) * 100
        Next x
        For y = 0 To 4
            rsd(0, y) = ""
        Next y
        rsd(0, 5) = Application.Max(Application.Index(rsd, , 6))
        MultiRSD = rsd
    End With
End Function

Book1
ABCDEF
1 6.994473
246434345463.400393
340434440455.429653
442413940433.856436
540424546445.549122
645464340406.483382
740404445396.494835
846444246415.20628
945464643414.90486
1041423939423.735407
1140454140405.262011
1246394042446.785697
1345404542465.756835
1444434346452.949865
1542414445414.264296
1645464642396.994473
1746414439436.342374
1842454239466.483382
1940444044414.902847
2045464443405.280213
2146444140445.696488
Sheet1
Cell Formulas
RangeFormula
A1:F21A1=MultiRSD(39,46,20)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,234
Members
449,092
Latest member
SCleaveland

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