Edit Randbetween macro to not include duplicates

ffionnah

Board Regular
Joined
Jun 12, 2018
Messages
61
Hi,
Could someone please help me edit this to make sure no duplicates are created?

Code:
 Sub RandPT_Ver2()Dim PT As Double, Num As Double
PT = InputBox("Enter The Total Unique Names", "Enter a Number")
Num = InputBox("Enter The 25% ", "Enter a Number")
For X = 1 To Num
    Range(ActiveCell, ActiveCell.Offset(0, 0)).Offset(X - 1) = Int((PT * rnd) + 1)
Next X
End Sub

Thank you!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Untested.
Code:
Sub RandPT_Ver2()
Dim PT As Variant, Num As Variant, d As Object, Try As Variant, ct As Long
Set d = CreateObject("Scripting.dictionary")
PT = InputBox("Enter The Total Unique Names", "Enter a Number")
If PT = "" Then Exit Sub
Num = InputBox("Enter The 25% ", "Enter a Number")
If Num = "" Then Exit Sub
Application.ScreenUpdating = False
For X = 1 To Num
    ct = 0
    Do
        ct = ct + 1
        Try = Int((PT * Rnd) + 1)
        If Not d.exists(Try) Then
            d.Add Try, d.Count + 1
            Range(ActiveCell, ActiveCell.Offset(0, 0)).Offset(X - 1) = Try
            Exit Do
        End If
    Loop While ct < 1000
Next X
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:

Code:
Sub RandPT_Ver3()
Dim PT As Double, Num As Double, Wk() As Double, Out() As Double, x as Long, y as Long

    PT = InputBox("Enter The Total Unique Names", "Enter a Number")
    Num = InputBox("Enter The 25% ", "Enter a Number")
    If Num > PT Then
        MsgBox "The sample size must be less than the total size."
        Exit Sub
    End If
    ReDim Wk(1 To PT)
    ReDim Out(1 To Num, 1 To 1)
    
    For x = 1 To PT
        Wk(x) = x
    Next x
    For x = 1 To Num
        y = Int(PT * Rnd) + 1
        Out(x, 1) = Wk(y)
        Wk(y) = Wk(PT)
        PT = PT - 1
    Next x
    
    ActiveCell.Resize(Num).Value = Out
    
End Sub

This version has been tested. It avoids having to look for previously used values by removing them from the set of possible choices, so there's no extra looping.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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