Cycle Count Generator

kumatsu

New Member
Joined
Mar 17, 2022
Messages
16
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am wanting to create a random pick generator that picks 15-20 numbers from a list without repeating and keeping track of the picked numbers and not picking them again in future picks. But will auto reset once all the items in the list have been picked. If possible.
 
Hi @kumatsu , check if the following macro meets what you require.
The first time, it selects 15 items at random, the next time it selects 15 items at random without repeating the previous ones, and so on. (I hope to meet @Joe4 's expectations 😬).

The first 15 records will be in cell I3 through I17, the next I18 through I32, and so on.

If you want to change the random number, change the 15 in this line:
num = 15 'random numbers

Try:
VBA Code:
Sub Select_Random_Names()
  Dim dic As Object
  Dim a As Variant, b As Variant, c As Variant, d As Variant
  Dim i As Long, j As Long, k As Long, lr2 As Long, x As Long, y As Long
  Dim arr As Variant, num As Long
  
  num = 15      'random numbers
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  lr2 = Range("I" & Rows.Count).End(3).Row
  If lr2 > 3 Then
    b = Range("I3:I" & lr2).Value
    If UBound(a, 1) = UBound(b, 1) Then
      MsgBox "All items have been selected. It will start again."
      Range("I3:I" & lr2).Cells.ClearContents
      lr2 = 2
    Else
      For i = 1 To UBound(b, 1)
        dic(b(i, 1)) = Empty
      Next
    End If
  End If
  ReDim c(1 To UBound(a, 1), 1 To 1)
  ReDim d(1 To num, 1 To 1)
  
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      j = j + 1
      c(j, 1) = a(i, 1)
    End If
  Next
  
  arr = Evaluate("ROW(1:" & j & ")")
  Randomize
  num = WorksheetFunction.Min(num, UBound(arr))
  
  For i = 1 To num
    x = Int(Rnd * j + i)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
    
    k = k + 1
    d(k, 1) = c(arr(i, 1), 1)
    j = j - 1
  Next i
  
  Range("I" & lr2 + 1).Resize(num).Value = d
End Sub
 
Upvote 0
Solution

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Dante, thanks for weighing in and helping out!
I came across some other posts on this forum where you were able to do similiar things (and pretty cool things!).
Sadly, I am not very proficient the Scripting Dictionary, and haven't done too much advanced work with arrays.
 
Upvote 0
Hi @kumatsu , check if the following macro meets what you require.
The first time, it selects 15 items at random, the next time it selects 15 items at random without repeating the previous ones, and so on. (I hope to meet @Joe4 's expectations 😬).

The first 15 records will be in cell I3 through I17, the next I18 through I32, and so on.

If you want to change the random number, change the 15 in this line:
num = 15 'random numbers

Try:
VBA Code:
Sub Select_Random_Names()
  Dim dic As Object
  Dim a As Variant, b As Variant, c As Variant, d As Variant
  Dim i As Long, j As Long, k As Long, lr2 As Long, x As Long, y As Long
  Dim arr As Variant, num As Long
 
  num = 15      'random numbers
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  lr2 = Range("I" & Rows.Count).End(3).Row
  If lr2 > 3 Then
    b = Range("I3:I" & lr2).Value
    If UBound(a, 1) = UBound(b, 1) Then
      MsgBox "All items have been selected. It will start again."
      Range("I3:I" & lr2).Cells.ClearContents
      lr2 = 2
    Else
      For i = 1 To UBound(b, 1)
        dic(b(i, 1)) = Empty
      Next
    End If
  End If
  ReDim c(1 To UBound(a, 1), 1 To 1)
  ReDim d(1 To num, 1 To 1)
 
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      j = j + 1
      c(j, 1) = a(i, 1)
    End If
  Next
 
  arr = Evaluate("ROW(1:" & j & ")")
  Randomize
  num = WorksheetFunction.Min(num, UBound(arr))
 
  For i = 1 To num
    x = Int(Rnd * j + i)
    y = arr(i, 1)
    arr(i, 1) = arr(x, 1)
    arr(x, 1) = y
   
    k = k + 1
    d(k, 1) = c(arr(i, 1), 1)
    j = j - 1
  Next i
 
  Range("I" & lr2 + 1).Resize(num).Value = d
End Sub

Good morning,

When I go to run it, I'm getting the following error

1657623959081.png
 
Upvote 0
Glad we were able to help!
And thanks for jumping on and saving the day, Dante!
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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