How to get random generator to pick from a list but delete the item it has picked

Alfaze

New Member
Joined
Jan 26, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Trying to get a list of 15 numbers randomly picked from a list of around 5000 for regular random stock counts, but once picked I don't want them to come up again. Any way to write the formula for this?

Currently I have a random generator set up using RANDBETWEEN function, but the issue is sometimes the same numbers come up again, unless i manually delete them out.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Doest till all numbers are exhausted there should not be repeation.
Then I don't think that you have tested that very well.
There is nothing in your code that records what was picked the time before last or any time before that.
This is how I tested your code.
I set up the sheet like below. Numbers in column A down to row 5001. Formula in D2 copied down to row 5001. This formula records if any of the 5,000 numbers is entered in B2:B16

I added the line shown right near the end of your code as well as the extra procedure shown at the bottom. Each time the code is run it copies the values from column D and adds them to any existing numbers in column E, thereby giving an updated count of how many times the numbers in column A have been chosen. If numbers are not repeated, the formula in E1 should not exceed 1.

Alfaze.xlsm
ABCDE
10
210
320
430
540
650
760
870
980
1090
11100
12110
13120
14130
15140
16150
17160
18170
19180
20190
Test
Cell Formulas
RangeFormula
E1E1=MAX(E2:E5000)
D2:D20D2=--ISNUMBER(MATCH(A2,B$2:B$16,0))



Rich (BB code):
Sub GetRandomNumbers()
Dim T&, Ta&, K&, A, B
Dim RstRng As Range
A = Range("A2:A5001")
Set RstRng = Range("B2:B16")

With CreateObject("Scripting.Dictionary")
For T = 1 To 15
Line1:
K = 0
K = WorksheetFunction.RandBetween(1, 5000)
If Not .exists(K) And WorksheetFunction.CountIf(RstRng, A(K, 1)) = 0 Then
.Add K, A(K, 1)
Else
GoTo Line1
End If
Next T
RstRng.Clear
RstRng.Value = Application.WorksheetFunction.Transpose(.items)

End With
UpdateCount Range("D2:D5001") '**********************************************************
End Sub

Sub UpdateCount(r As Range)
  With r
    .Copy
    .Offset(, 1).PasteSpecial Paste:=xlValues, Operation:=xlAdd
  End With
  Application.CutCopyMode = False
End Sub

Here is the sheet again after I have run the code 7 times. Cell E1 shows that at least one number has been selected twice in those 7 runs (happened to be the number 3004 in this test)
I have cleared E2:E5002 and repeated the test several times. On one occasion a number doubled up after only 3 runs but mostly it happened between about 7 to 15 runs - but certainly well before all 5000 numbers were used once.

Alfaze.xlsm
ABCDE
12
21387200
32419700
43450600
54400000
65173800
76300400
87262500
98446200
10997100
111052100
1211281300
1312119800
14132801
1514110600
1615494900
171600
181700
191800
201901
Test
Cell Formulas
RangeFormula
E1E1=MAX(E2:E5000)
D2:D20D2=--ISNUMBER(MATCH(A2,B$2:B$16,0))
 
Upvote 0
This code does not repeat any number till all numbers are exhausted.
Helper column Z is used. If it not free in your file change it some other free column. Change "Z" in all lines in the code.
VBA Code:
Sub GetRandomNumbers()
Dim T&, Ta&, K&, Alr, Lr&, Lr2&, A, B
Dim RstRng As Range
Alr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A2:A" & Alr)
Set RstRng = Range("B2:B16")
Range("Z1") = "Helper Column"

Lr = Range("Z" & Rows.Count).End(xlUp).Offset(1, 0).Row
Lr2 = Lr - 1
With CreateObject("Scripting.Dictionary")
For T = 1 To 15
Line1:
K = 0
K = WorksheetFunction.RandBetween(1, Alr - 1)
If Not .exists(K) And WorksheetFunction.CountIf(Range("Z2:Z" & Lr), A(K, 1)) = 0 Then
.Add K, A(K, 1): Range("Z" & Lr) = A(K, 1)
    If Lr - 1 = UBound(A, 1) Then
    Range("Z2:Z" & Lr2).Delete (xlUp)
    Lr = Range("Z" & Rows.Count).End(xlUp).Row
    End If
Lr = Lr + 1
Else
GoTo Line1
End If
Next T
RstRng.Clear
RstRng.Value = Application.WorksheetFunction.Transpose(.items)

End With
End Sub
 
Upvote 0
Solution
This code does not repeat any number till all numbers are exhausted.
Helper column Z is used. If it not free in your file change it some other free column. Change "Z" in all lines in the code.
VBA Code:
Sub GetRandomNumbers()
Dim T&, Ta&, K&, Alr, Lr&, Lr2&, A, B
Dim RstRng As Range
Alr = Range("A" & Rows.Count).End(xlUp).Row
A = Range("A2:A" & Alr)
Set RstRng = Range("B2:B16")
Range("Z1") = "Helper Column"

Lr = Range("Z" & Rows.Count).End(xlUp).Offset(1, 0).Row
Lr2 = Lr - 1
With CreateObject("Scripting.Dictionary")
For T = 1 To 15
Line1:
K = 0
K = WorksheetFunction.RandBetween(1, Alr - 1)
If Not .exists(K) And WorksheetFunction.CountIf(Range("Z2:Z" & Lr), A(K, 1)) = 0 Then
.Add K, A(K, 1): Range("Z" & Lr) = A(K, 1)
    If Lr - 1 = UBound(A, 1) Then
    Range("Z2:Z" & Lr2).Delete (xlUp)
    Lr = Range("Z" & Rows.Count).End(xlUp).Row
    End If
Lr = Lr + 1
Else
GoTo Line1
End If
Next T
RstRng.Clear
RstRng.Value = Application.WorksheetFunction.Transpose(.items)

End With
End Sub
Thanks, that is perfect 👍
 
Upvote 0
I didn't try it, post 10 just looked a bit simpler so I went with that!
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

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