Forum Discussion

wayne gasson's avatar
wayne gasson
Copper Contributor
Oct 25, 2018

VBA Random Number

Hi All,

I have created a Random Number Generator using the code below

Private Sub CommandButton1_Click()

Range("d2") = WorksheetFunction.RandBetween(-2, 2)
Range("d3") = WorksheetFunction.RandBetween(-2, 2)

which works exactly how I want it to

What I would like to do from here is generate a second button code to produce a 2nd random number using the first rand number as it's lowest value - but the code I thought would work doesn't - can anyone help please.

The code I used:
Range("f2") = WorksheetFunction.RandBetween(D2, 2)
Range("f3") = WorksheetFunction.RandBetween(D3, 2)

  • You may consider the following:

     

    Range("F1") = WorksheetFunction.RandBetween(-2,2)   ' The first random number

    k = Range("F1")

    Range("F2") = WorkhseetFunction.RandBetween (k,2) 

    ' The second random number which lower bound depends on the previous random number

     

    Hop it helps. 

  • Man Fai Chan's avatar
    Man Fai Chan
    Iron Contributor

    You may consider the following:

     

    Range("F1") = WorksheetFunction.RandBetween(-2,2)   ' The first random number

    k = Range("F1")

    Range("F2") = WorkhseetFunction.RandBetween (k,2) 

    ' The second random number which lower bound depends on the previous random number

     

    Hop it helps. 

Resources