Reset random list-name generator

jammoca

Banned
Joined
Nov 6, 2002
Messages
1,100
I am using the following code in a userform (via a command button called "NextName") to display (in a Label called "StudentName" ) a student name randomly chosen from a list in column A .......

Code:
Option Explicit

Sub Initialize(aWS As Worksheet)
    With aWS
    Dim aRng As Range
    Set aRng = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
        End With
    With aRng
    .Copy
    .Offset(0, 2).PasteSpecial xlPasteValues
    With .Offset(0, 3)
    .FormulaR1C1 = "=RAND()"
    .Value = .Value
        End With
    .Offset(0, 2).Resize(, 2).Sort Key1:=.Offset(0, 3).Cells(1, 1), Order1:=xlAscending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    .Offset(0, 3).EntireColumn.Delete
    With .Offset(0, 3)
    .Cells(1, 1).Value = 0
    .Cells(2, 1).Value = aRng.Cells.Count
        End With
        End With
    End Sub
Private Sub NextName_Click()
    Dim aWS As Worksheet
    Set aWS = ActiveSheet
    With aWS
        If IsEmpty(.Cells(1, 3).Value) Then Initialize aWS
        .Cells(1, 4).Value = .Cells(1, 4).Value + 1
        If .Cells(1, 4).Value > .Cells(2, 4).Value Then MsgBox "All students have been listed": Exit Sub
        Me.StudentName.Caption = .Columns(3).Cells(.Cells(1, 4).Value)
        End With
    End Sub

However, after making it's way through the list once, it won't proceed as it has exhausted the list.

I would like to change the code so it resets itself when it finishes going through the list.

So after passing through the list once, I'd like the list to be randomised again, and names to be permitted to continue being displayed in the Label.
 
Last edited by a moderator:

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,517
Do you want the message "All students have been listed" to be displayed in between exhausting the list and starting again?

If so, just change
Code:
        If .Cells(1, 4).Value > .Cells(2, 4).Value Then MsgBox "All students have been listed": Exit Sub
to be:
Code:
        If .Cells(1, 4).Value > .Cells(2, 4).Value Then MsgBox "All students have been listed": Columns(3).ClearContents: Exit Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,327
Members
414,053
Latest member
Dual Showman

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
Top