VBA RND Function (Syntax + Example)

Last Updated: October 29, 2023
puneet-gogia-excel-champs

- Written by Puneet

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 the VBA RND function, you need to go through the below example where we have written a vba code by using it:

example-vba-rnd-function
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 A1, A2, and A3. And if we re-run this it will return the different numbers in all three cells.