Unique Random Between

theboyscout

New Member
Joined
Nov 29, 2012
Messages
26
I'm creating a unique number for new users of a list of 3000. What I would like to do is create random numbers which do not already exist in the list of prior users.

Something like:
randombetween(100000, 999999) <> A3

Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Change A3 to A4 by the range of cells where you already have your numbers. Change 30 to the number of random numbers you want. The result will be in column B


Code:
Sub Unique_Random_Between()
'
    Dim num As New Collection
    On Error Resume Next
    n = [COLOR=#0000ff]30[/COLOR]      'change to 3000
    Set rango = Range("[COLOR=#0000ff]A3:A4[/COLOR]")  'numbers already exist
    For Each celda In rango
        valor = celda.Value
        num.Add Item:=valor, Key:=CStr(valor)
    Next
    Do While num.Count < n
        valor = WorksheetFunction.RandBetween(100000, 999999)
        num.Add Item:=valor, Key:=CStr(valor)
         
    Loop
    For i = 1 To num.Count
        Cells(i, "B").Value = num(i)
    Next
    MsgBox "End"
End Sub
 
Last edited:
Upvote 0
Why bother with random numbers for this kind of thing ?
Why not allocate numbers sequentially from the range ?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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