Generate a Non-Repeating List of Random Numbers in Excel - UDF

Add to Favorites

Generate a series of non-repeating random numbers in Excel with this UDF (user defined function). This is a great function to use with statistical analysis and any other situation where you need to generate non-repeating random numbers. This is a very easy to use function and only has three arguments: one for the lowest possible random number; one for the highest possible random number; and one for the number of random numbers which you would like to display.

The one thing to note with this macro is that it will display all of the random numbers in the same cell as the function. The random numbers are separated by a space and this makes it easy to use text-manipulation functions, or the extract text UDF from this site, to pull the randomly generated numbers out of this cell.

Where to install the macro:  Module

UDF to Generate a Non-Repeating List of Random Numbers in Excel

Function RANDNUMNOREP(Bottom As Integer, Top As Integer, Amount As Integer) As String
'This UDF will generate a non-repeating set of random numbers in excel and display those in the same cell as the function

Dim iArr As Variant
Dim i As Integer
Dim r As Integer
Dim temp As Integer


ReDim iArr(Bottom To Top)

For i = Bottom To Top
 iArr(i) = i
Next i

For i = Top To Bottom + 1 Step -1
 r = Int(Rnd() * (i - Bottom + 1)) + Bottom
 temp = iArr(r)
 iArr(r) = iArr(i)
 iArr(i) = temp
Next i

For i = Bottom To Bottom + Amount - 1
Next i


End Function

How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.

Tutorial Details
Excel Forum