Random Number Generator For Each Cell in a Selected Range

Tanner_2004

Well-known Member
Joined
Jun 1, 2010
Messages
616
This code works great, but it stops after producing 10 random numbers. Can someone please tell me how to modify so that it produces a random number for every cell within an selected range? Thank you.

Code:
Sub randomNumbers()    Low = Application.InputBox("Enter first valid value", Type:=1)
    High = Application.InputBox("Enter last valid value", Type:=1)
    Selection.Clear
    For Each cell In Selection.Cells
        If WorksheetFunction.CountA(Selection) = (High - Low + 1) Then Exit For
        Do
            rndNumber = Int((High - Low + 1) * Rnd() + Low)
        Loop Until Selection.Cells.Find(rndNumber, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
        cell.Value = rndNumber
    Next
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe:

Code:
Sub randomNumbers()

    Low = Application.InputBox("Enter first valid value", Type:=1)
    High = Application.InputBox("Enter last valid value", Type:=1)
   
    For Each cell In Selection.Cells
        cell.Value = Int((High - Low + 1) * Rnd() + Low)
    Next

End Sub

The original code had a check to make sure that no random number was repeated. I can add that if you want, but it would depend on the selection size.
 
Last edited:
Upvote 0
What are your High and Low inputs, and how many cells in your Selection when the code stops at 10?
 
Upvote 0
You're welcome!

If you do ever want the "no repeats" version, try:

Code:
Sub randomNumbers()
Dim Low As Long, High As Long, UsedNums As Object, work As Long


    Set UsedNums = CreateObject("Scripting.Dictionary")
    
    Low = Application.InputBox("Enter first valid value", Type:=1)
    High = Application.InputBox("Enter last valid value", Type:=1)
    
    If Selection.Cells.Count > (High - Low) + 1 Then
        MsgBox "Not enough cells for unique random numbers."
        Exit Sub
    End If
   
    For Each cell In Selection.Cells
        Do
            work = Int((High - Low + 1) * Rnd() + Low)
        Loop Until Not UsedNums.exists(work)
        UsedNums.Add work,1
        cell.Value = work
    Next


End Sub
 
Upvote 0
Would you know how to enter a random percentage? I changed tge type to 14, but it gives me 0 or 100%. Please and thank you.
 
Upvote 0
On the first macro, try

cell.Value = (Int((High - Low + 1) * Rnd() + Low)) / 100

On the second, try:


cell.Value = work / 100

and make sure to format the cells as Percentage.</pre>
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top