Pick Random From List, Then Delete

redhots4

Board Regular
Joined
Aug 30, 2004
Messages
136
Office Version
  1. 365
Platform
  1. MacOS
Hi. I have a list of 365 writing prompts, one for each day of the year. I want to randomly select a number (1-365), then look up the prompt for that number. Once that prompt is picked, I want to delete its corresponding number from the list of available numbers so it doesn't get picked again. Anyone done this or have a good solution?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
There must be a half-dozen ways to get a random value from a list. Did you look at any of them? Here's one method:

Not sure what "I want" means here. It might mean that you will manually look up and then delete from the list.
 
Upvote 0
There must be a half-dozen ways to get a random value from a list. Did you look at any of them? Here's one method:

Not sure what "I want" means here. It might mean that you will manually look up and then delete from the list.
Thanks for the reply. I'm well versed in picking random numbers between lower and upper limits. What I need here is (a.) the ability to click a button to pick a number randomly from a list of previously-unused numbers and (b) the ability to instruct Excel to delete a picked number from the list of remaining numbers so it isn't picked again. Deleting the used numbers gives me the ability to visually see the list of prompts dwindling over time.
 
Upvote 0
To understand better and for testing, could we reduce the initial number to 10 instead of 365, post that sample data with XL2BB so that we can see the layout and copy for testing? You might further explain in words, just what would happen on the sheet after each pick.
 
Upvote 0
I'm well versed in picking random numbers between lower and upper limits
If by that you mean between x and y numbers, that's not what I intended to suggest by the link. It was supposed to suggest how to choose a value from a list of values. In effect, they're not really "unused" when values are being removed. They either exist in the list, or not. Once randomly chosen and the prompt has been looked up, the code deletes the list value, I guess. I can't see how generating a random number between 1 and 365 and looking that up in a list will ever work if anyone suggests that, because you'll eventually generate values that don't exist in the list. Hence, picking a random list value is part of the solution I'd say.
 
Upvote 0
To understand better and for testing, could we reduce the initial number to 10 instead of 365, post that sample data with XL2BB so that we can see the layout and copy for testing? You might further explain in words, just what would happen on the sheet after each pick.
Good idea. Here's a sample sheet with only ten rows. The formula in C2 returns a value at random from column A. How do I tell Excel that the value chosen at random should not be available for selection again? Ideally, I'd like to copy the value in C2 and append it to a list on another tab with the date, so I could see a list of which prompts I randomly selected by date. (Ignore the conditional formatting error).

365 Gratitude Prompts.xlsm
ABC
1ListRandom
2Writing Prompt 1Writing Prompt 12
3Writing Prompt 2
4Writing Prompt 3
5Writing Prompt 4
6Writing Prompt 5
7Writing Prompt 6
8Writing Prompt 7
9Writing Prompt 8
10Writing Prompt 9
11Writing Prompt 10
12Writing Prompt 11
13Writing Prompt 12
14Writing Prompt 13
15Writing Prompt 14
16Writing Prompt 15
17Writing Prompt 16
18Writing Prompt 17
19Writing Prompt 18
20Writing Prompt 19
21Writing Prompt 20
22Writing Prompt 21
23Writing Prompt 22
24Writing Prompt 23
25Writing Prompt 24
Sheet1
Cell Formulas
RangeFormula
C2C2=INDEX(A2:A25,RANDBETWEEN(2,COUNTA(A2:A25)))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A25Expression=#REF!=242textNO
 
Upvote 0
Here's a sample sheet with only ten rows.
Hmm, an interesting count of 10! ;)
.. but thanks for getting XL2BB going. (y)

EDIT:
On re-reading, I think that you have answered the following so I will post back in a while.


What about this part
You might further explain in words, just what would happen on the sheet after each pick.
.. or else another XL2BB to show what the sheet would look like after "Writing Prompt 12" has been used?
 
Upvote 0
With Sheet1 as you have shown above. Name a blank sheet in the workbook as 'Log' and try running this code repeatedly.
At any point you can look at the Log sheet to see which prompts have been used and which ones have not been used.

VBA Code:
Sub Random_Picks()
  Dim wsFull As Worksheet, wsLog As Worksheet
  Dim Pick As Long
  
  Set wsFull = Sheets("Sheet1")
  Set wsLog = Sheets("Log")
  Randomize
  wsFull.Activate
  With wsLog
    If IsEmpty(.Range("A1").Value) Then
      wsFull.Range("A2", wsFull.Range("A" & Rows.Count).End(xlUp)).Copy Destination:=.Range("A2")
      .Range("A1:B1").Value = Array("Unused", "Used")
    End If
    If IsEmpty(.Range("A2").Value) Then
      wsFull.Range("C2").ClearContents
      .UsedRange.ClearContents
      MsgBox "All used. Prompts have been reset. Try again"
    Else
      Pick = 2 + Int(Rnd() * (.Range("A" & Rows.Count).End(xlUp).Row - 1))
      wsFull.Range("C2").Value = .Range("A" & Pick).Value
      .Range("B" & Rows.Count).End(xlUp).Offset(1).Value = .Range("A" & Pick).Value
      .Cells(Pick, "A").Delete Shift:=xlUp
    End If
  End With
End Sub
 
Upvote 0
With Sheet1 as you have shown above. Name a blank sheet in the workbook as 'Log' and try running this code repeatedly.
At any point you can look at the Log sheet to see which prompts have been used and which ones have not been used.

VBA Code:
Sub Random_Picks()
  Dim wsFull As Worksheet, wsLog As Worksheet
  Dim Pick As Long
 
  Set wsFull = Sheets("Sheet1")
  Set wsLog = Sheets("Log")
  Randomize
  wsFull.Activate
  With wsLog
    If IsEmpty(.Range("A1").Value) Then
      wsFull.Range("A2", wsFull.Range("A" & Rows.Count).End(xlUp)).Copy Destination:=.Range("A2")
      .Range("A1:B1").Value = Array("Unused", "Used")
    End If
    If IsEmpty(.Range("A2").Value) Then
      wsFull.Range("C2").ClearContents
      .UsedRange.ClearContents
      MsgBox "All used. Prompts have been reset. Try again"
    Else
      Pick = 2 + Int(Rnd() * (.Range("A" & Rows.Count).End(xlUp).Row - 1))
      wsFull.Range("C2").Value = .Range("A" & Pick).Value
      .Range("B" & Rows.Count).End(xlUp).Offset(1).Value = .Range("A" & Pick).Value
      .Cells(Pick, "A").Delete Shift:=xlUp
    End If
  End With
End Sub
Works beautifully, thank you!! Could you make a minor edit to enter the date & time in Column C, alongside "Used" to indicate the date / time each line was added to the Used log?
 
Upvote 0
Works beautifully, thank you!! Could you make a minor edit to enter the date & time in Column C, alongside "Used" to indicate the date / time each line was added to the Used log?
Try this

VBA Code:
Sub Random_Picks_v2()
  Dim wsFull As Worksheet, wsLog As Worksheet
  Dim Pick As Long
  
  Set wsFull = Sheets("Sheet1")
  Set wsLog = Sheets("Log")
  Randomize
  wsFull.Activate
  With wsLog
    If IsEmpty(.Range("A1").Value) Then
      wsFull.Range("A2", wsFull.Range("A" & Rows.Count).End(xlUp)).Copy Destination:=.Range("A2")
      .Range("A1:C1").Value = Array("Unused", "Used", "Date/Time")
    End If
    If IsEmpty(.Range("A2").Value) Then
      wsFull.Range("C2").ClearContents
      .UsedRange.ClearContents
      MsgBox "All used. Prompts have been reset. Try again"
    Else
      Pick = 2 + Int(Rnd() * (.Range("A" & Rows.Count).End(xlUp).Row - 1))
      wsFull.Range("C2").Value = .Range("A" & Pick).Value
      .Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(, 2).Value = Array(.Range("A" & Pick).Value, Now())
      .Cells(Pick, "A").Delete Shift:=xlUp
    End If
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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