Use Rnd with Int to get a whole number between two values, and run Randomize first so the numbers change each time Excel starts:
Randomize
Range("A1").Value = Int((45 - 10 + 1) * Rnd + 10) ' 10 to 45
The pattern is Int((upper - lower + 1) * Rnd + lower). Rnd on its own returns a decimal from 0 up to, but not including, 1.
WorksheetFunction.RandBetween(10, 45) gives the same result with less to type. To fill a block in one step, Range("A1:B10").Value = WorksheetFunction.RandArray(10, 2, 1000, 2000, True) returns whole numbers from 1,000 to 2,000. RandArray needs Excel 365 or 2021 and fails with error 438 in older versions.
Range("A1:A10").Value = Rnd puts the same number in every cell, because Rnd runs once. Loop through the cells to get a different number in each, as in For Each c In Range("A1:A10"): c.Value = Rnd: Next c.
Random numbers can repeat. For a draw with no repeats, such as picking 5 winners from 50 entries, store each number as you pick it and pick again when it comes up twice, or shuffle the full list and take the first five.
Use Int, not Round or CInt. Rounding makes the lowest and highest values come up half as often as the others, so the results look random but are not evenly spread.
In VBA, there are different ways that you can use to generate a random number in Excel, and in this post, we will look at all of them one by one.
RND Function
To generate a random number, in VBA, there is a function called RND. This function stands for random, and when you use this function in code, it returns a random decimal number that is 0 or more and less than 1. In RND, you don’t need to specify any argument.
Range("A1") = Rnd()
The above code uses it to get a random number in cell A1, and it has returned 0.705547521.

But when you use it for an entire range, it returns the same random number in the entire range, and that makes it useless when you want a different random number in each cell.

Now here it doesn’t make sense to use it, right? But there’s a solution that you can use: FOR NEXT LOOP.

In the above code, you have a loop that goes through 10 cells (one by one), starting from the selected cell, and adds a random number to each.
Here’s the full code:
Sub vba_random_number() Dim i As Long i = 10 For i = 1 To i ActiveCell.Value = Rnd() ActiveCell.Offset(1, 0).Select Next i End Sub
Random Number Between Two Numbers
If you want to generate a random number between two defined numbers, in that case, you need to write code using two VBA Functions (RND + INT). The code for this formula would be like below.

Sub vba_random_number() Dim myRnd As Integer myRnd = Int(10 + Rnd * (45 - 10 + 1)) Range("A1") = myRnd End Sub
When you run this macro, RND uses the max number (45) and the min number (10) that you have defined to generate the random number, and INT removes the decimal from it. This follows Microsoft’s formula Int((max – min + 1) * Rnd + min), which gives a whole number from the min number to the max number.

Using Worksheet Functions in a VBA Code to Generate a Random Number
In VBA, you can access worksheet functions and use them to get a result by specifying the arguments. There are two functions from the worksheet that can help you get a random number in the result.
- RandBetween
- RandArray
Before you use these functions, make sure to understand the arguments that you need to define. Now in the below code, you have RANDBETWEEN which generates a random number between two numbers.

Range("A1") = WorksheetFunction.RandBetween(1000, 2000)
Here the max number is 2000 and the min number is 1000. In the same way, you can use RANDARRAY, which is a dynamic array function. It’s only available in Microsoft 365 and Excel 2021 or later, so this code gives an error in older versions.
Here’s the code.
Range("A1:B10") = WorksheetFunction.RandArray(10, 2, 1000, 2000, True)

If you look at the syntax of RANDARRAY (rows, columns, min, max, whole number), you will find that you can enter random numbers in an entire range in one go, which is not possible with the other methods that we have discussed above. Here, TRUE in the last argument returns whole numbers.

Use Randomize to Get a New Set of Numbers
There’s one thing about RND that surprises many people: every time you open Excel, RND starts from the same point, so it gives you the same sequence of numbers. To get a new sequence each time, add the Randomize statement once at the start of your code. It uses the system timer as a new starting point:
Sub random_numbers_in_range()
Dim c As Range
Randomize
For Each c In Range("A1:A10")
c.Value = Int((45 - 10 + 1) * Rnd + 10)
Next c
End Sub
This code also fills A1:A10 with whole numbers between 10 and 45 without selecting any cell, which is faster than moving the active cell in the loop.
Frequently Asked Questions
Why do I get the same random numbers every time?
Because you haven’t used Randomize. Without it, RND gives the same sequence each time Excel starts. Add Randomize once before you call RND.
How do I get a random decimal number between two numbers?
Leave out INT: min + Rnd * (max – min) gives a decimal number from the min number up to (but not including) the max number, like 10 + Rnd * 35 for 10 to 45.
Do the random numbers change when the sheet recalculates?
No. A number that VBA writes into a cell is a fixed value. Only the RAND and RANDBETWEEN worksheet formulas change with every recalculation.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.