How to use the VBA RND Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA RND Function (Syntax + Example)

The VBA RND function is listed under the math category of VBA functions. When you use it in a VBA code, it generates a random number that is greater than or equals to zero and lower than or equal to 1. In simple words, it returns a random number between 0 to 1. It works like the RAND function in the worksheet.

Syntax

Rnd([Number])

Arguments

  • [Number]: An optional numeric argument where you can specify [This is an optional argument and if omitted VBA takes >0 by default]:
    1. <0 to get the same random number on each call, using [Number] as the seed number.
    2. =0 to get the most recently generated random number.
    3. >0 to get the next random number in the sequence.

Example

To practically understand how to use VBA RND function, you need to go through the below example where we have written a vba code by using it:

Sub example_RND()
Range("A1").Value = Rnd()
Range("A2").Value = Rnd()
Range("A3").Value = Rnd()
End Sub

In the above code, we have used RND to get random numbers in the A1, A2, and A3. And if we re-run this it will return the different numbers in all three cells.

Related Functions

ABS | ATN | COS | EXP | FIX | INT | LOG | ROUND | SGN | SIN | SQR | TAN