VBA Random Number

Associated Files Download Links

RND Function

The RND Function generates a number that is between 0 and 1. The syntax of the RND Function is:

Rnd([Number]) where:

  • Number (Optional) – This is optional and if <0, the function returns the same random number on each call using [Number] as the seed, if =0, the function returns the most recent random number, if >0 the function returns the next generated random number. If blank the default >0, is used.

Generating a Random Number in VBA

In order to generate a random number between two values, you have to use the RND Function in combination with the INT Function  (Integer Function) using the following general formula:

  • Int(lowerbound + Rnd * ( upperbound – lowerbound + 1 ) )

So, in order to generate a random number between 2 and 30, you would use the following code:

 

 

You may also like some of this related content...

Ads