=RAND() Function use question

Desu Nota from Columbus

Well-known Member
Joined
Mar 17, 2011
Messages
556
I have the immediate need to generate the integers 1-8 in cells A22-A29 randomly without duplicating numbers.

Is there a way to do this with a formula or does this require a macro?

Also, if it isn't obvious within the solution, how would you change the formula/macro to account for a different range of integers? For instance 1-10, 5-50, etc.

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
randbetween limits results to a range (check in the analysis toolpak)

Code:
/Dim Low As Double
Dim High As Double
Low = 11 '<<< CHANGE AS DESIRED
High = 20 '<<< CHANGE AS DESIRED
R = Int((High - Low + 1) * Rnd() + Low)
 
Last edited:
Upvote 0
For random numbers within a range of numbers without repetition try selecting a range and running

Code:
Sub rdm()
Dim cell As Range, MyRanRng, x, nDec As Integer
Dim K As Long, iFlag As Boolean
Dim i As Integer, j As Integer, n As Integer
Dim NosAvailable As Long
Dim ArrayOfValues
MyRanRng = Application.InputBox(prompt:="Enter lower and upper limits separated by space", _
    Title:="Enter number range")
If TypeName(MyRanRng) = "Boolean" Then Exit Sub
x = Split(MyRanRng)
If UBound(x) = 0 Then Exit Sub
nDec = Application.InputBox(prompt:="No. decimal places", Title:="Enter decimal places", Type:=1)
If TypeName(nDec) = "Boolean" Then nDec = 0
K = Selection.Cells.Count
NosAvailable = (x(UBound(x)) - x(LBound(x))) * 10 ^ nDec + 1
If K > NosAvailable Then
    MsgBox prompt:="Cells available:" & vbTab & K & vbCrLf & "Numbers available:" & _
        vbTab & NosAvailable & vbCrLf & vbCrLf & _
        "Select a smaller range or increase the number range", _
        Title:="Error trap!", Buttons:=vbOKOnly + vbCritical
    Exit Sub
End If
Randomize
ReDim ArrayOfValues(1 To K) As Variant
For i = 1 To K
    Do
        iFlag = False
        ArrayOfValues(i) = Round(Rnd() * (x(UBound(x)) - x(LBound(x))) + x(LBound(x)), nDec)
        For n = 1 To i - 1
            If ArrayOfValues(i) = ArrayOfValues(n) Then iFlag = True
        Next n
    Loop Until iFlag = False
Next i
j = 0
For Each cell In Selection
    j = j + 1
    cell.Value = ArrayOfValues(j)
Next cell
End Sub
 
Upvote 0
This UDF should do what you want.
Select A22:A29 and enter the array formula =RndValues(8)
(This should be entered with Ctl-Shift-Enter or Cmd+Return for Mac)

Code:
Function RndValues(Optional Size As Long, Optional LowValue As Long = 1) As Variant
    Dim Result As Variant
    Dim i As Long, randIndex As Long, temp As Long
    Application.Volatile
    If Size < 1 Then
        Size = Application.Caller.Cells.Count
    End If
    
    ReDim Result(1 To Size)
    For i = 1 To Size
        Result(i) = i + LowValue - 1
    Next i
    
    For i = 1 To Size
        randIndex = Int(Rnd() * Size) + 1
        temp = Result(i)
        Result(i) = Result(randIndex)
        Result(randIndex) = temp
    Next i
    
    RndValues = Result
End Function
For values from 1-10, use the formula =RndValues(10)
For values from 5-50, use the formula =RndValues(46, 5)
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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