#If VBA7 Then
Public Declare PtrSafe Function ModelessMsgBox Lib "User32" Alias "MessageBoxA" (Optional ByVal hWnd As Long, _
Optional ByVal prompt As String, _
Optional ByVal title As String, _
Optional ByVal buttons As Long) As Long
#Else
Public Declare Function ModelessMsgBox Lib "User32" Alias "MessageBoxA" (Optional ByVal hWnd As Long, _
Optional ByVal prompt As String, _
Optional ByVal title As String, _
Optional ByVal buttons As Long) As Long
#End If
Sub Name_Prize_GeneratorV2()
'
Dim LastRow As Long
Dim RandomEmployeeNumberPicked As Long, RandomEmployeeNumberPickedCounter As Long, RandomNumberGeneratedCounter As Long
Dim RandomEmployeeNumberPickedArray As Object
Dim EmployeeArray As Variant
Dim wsSource As Worksheet
'
Set wsSource = Sheets("Sheet3") ' <--- Set this to the sheet name that contains the employee list
'
LastRow = wsSource.Range("B" & Rows.Count).End(xlUp).Row ' Get Last Row Number of employee names
'
EmployeeArray = wsSource.Range("B2:B" & LastRow) ' Load Employee names into a 2D 1 based array RC
'
Set RandomEmployeeNumberPickedArray = CreateObject("Scripting.Dictionary") ' Establish Dictionary Array
'
RandomEmployeeNumberPickedCounter = 1 ' Initialize RandomEmployeeNumberPickedCounter
'
wsSource.Range("F2:S52").ClearContents ' Erase the Results area
'
'
' '-----------------------------'
' ' Generate the random numbers '
' '-----------------------------'
'
While RandomEmployeeNumberPickedArray.Count < 100 ' Establish loop to generate 100 unique random numbers ... 1 to 100
RandomEmployeeNumberPicked = Application.WorksheetFunction.RandBetween(1, 100) ' Generate random number between 1 & 100
'
If Not RandomEmployeeNumberPickedArray.Exists(RandomEmployeeNumberPicked) Then ' If this is a unique random # then ...
RandomEmployeeNumberPickedArray.Add RandomEmployeeNumberPicked, RandomEmployeeNumberPickedCounter ' Save the number into dictionary array
RandomEmployeeNumberPickedCounter = RandomEmployeeNumberPickedCounter + 1 ' Increment the RandomEmployeeNumberPickedCounter
End If
Wend ' Loop back
'
'
' '-----------------------------'
' ' Display Drawing # 1 Results '
' '-----------------------------'
'
For RandomNumberGeneratedCounter = 1 To 50 ' Establish loop to loop through first 50 #s randomly generated
wsSource.Range("F" & RandomNumberGeneratedCounter + 1).Value = RandomNumberGeneratedCounter ' Display counter of 1 - 50
'
' Display associated names to those random #s generated
wsSource.Range("G" & RandomNumberGeneratedCounter + 1).Value = EmployeeArray(RandomEmployeeNumberPickedArray.Keys()(RandomNumberGeneratedCounter - 1), 1)
Next ' Loop Back
'
For I = 1 To 5 ' Use a loop to allow time for results to display ... increase if needed
DoEvents
Next I
'
If ModelessMsgBox(prompt:=Space(27) & "$100 Prize winners have have been displayed." & vbCrLf & vbCrLf & "Press the 'OK' button when you " & _
"are ready to display the $200 prize winners.", title:=Space(33) & "Program paused to allow scrolling.", buttons:=vbOK) <> 1 Then Exit Sub
'
'
' '-----------------------------'
' ' Display Drawing # 2 Results '
' '-----------------------------'
'
For RandomNumberGeneratedCounter = 1 To 25 ' Establish loop to loop through next 25 #s randomly generated
wsSource.Range("I" & RandomNumberGeneratedCounter + 1).Value = RandomNumberGeneratedCounter ' Display counter of 1 - 25
'
' Display associated names to those random #s generated
wsSource.Range("j" & RandomNumberGeneratedCounter + 1).Value = EmployeeArray(RandomEmployeeNumberPickedArray.Keys()(RandomNumberGeneratedCounter + 49), 1)
Next ' Loop Back
'
For I = 1 To 5 ' Use a loop to allow time for results to display ... increase if needed
DoEvents
Next I
'
If ModelessMsgBox(prompt:=Space(27) & "$200 Prize winners have have been displayed." & vbCrLf & vbCrLf & "Press the 'OK' button when you " & _
"are ready to display the $300 prize winners.", title:=Space(33) & "Program paused to allow scrolling.", buttons:=vbOK) <> 1 Then Exit Sub
'
'
' '-----------------------------'
' ' Display Drawing # 3 Results '
' '-----------------------------'
'
For RandomNumberGeneratedCounter = 1 To 15 ' Establish loop to loop through next 15 #s randomly generated
wsSource.Range("L" & RandomNumberGeneratedCounter + 1).Value = RandomNumberGeneratedCounter ' Display counter of 1 - 15
'
' Display associated names to those random #s generated
wsSource.Range("M" & RandomNumberGeneratedCounter + 1).Value = EmployeeArray(RandomEmployeeNumberPickedArray.Keys()(RandomNumberGeneratedCounter + 74), 1)
Next ' Loop Back
'
For I = 1 To 5 ' Use a loop to allow time for results to display ... increase if needed
DoEvents
Next I
'
If ModelessMsgBox(prompt:=Space(27) & "$300 Prize winners have have been displayed." & vbCrLf & vbCrLf & "Press the 'OK' button when you " & _
"are ready to display the $400 prize winners.", title:=Space(33) & "Program paused to allow scrolling.", buttons:=vbOK) <> 1 Then Exit Sub
'
'
' '-----------------------------'
' ' Display Drawing # 4 Results '
' '-----------------------------'
'
For RandomNumberGeneratedCounter = 1 To 8 ' Establish loop to loop through next 8 #s randomly generated
wsSource.Range("O" & RandomNumberGeneratedCounter + 1).Value = RandomNumberGeneratedCounter ' Display counter of 1 - 8
'
' Display associated names to those random #s generated
wsSource.Range("P" & RandomNumberGeneratedCounter + 1).Value = EmployeeArray(RandomEmployeeNumberPickedArray.Keys()(RandomNumberGeneratedCounter + 89), 1)
Next ' Loop Back
'
For I = 1 To 5 ' Use a loop to allow time for results to display ... increase if needed
DoEvents
Next I
'
If ModelessMsgBox(prompt:=Space(27) & "$400 Prize winners have have been displayed." & vbCrLf & vbCrLf & "Press the 'OK' button when you " & _
"are ready to display the $500 prize winners.", title:=Space(33) & "Program paused to allow scrolling.", buttons:=vbOK) <> 1 Then Exit Sub
'
'
' '-----------------------------'
' ' Display Drawing # 5 Results '
' '-----------------------------'
'
For RandomNumberGeneratedCounter = 1 To 2 ' Establish loop to loop through last 2 #s randomly generated
wsSource.Range("R" & RandomNumberGeneratedCounter + 1).Value = RandomNumberGeneratedCounter ' Display counter of 1 - 2
'
' Display associated names to those random #s generated
wsSource.Range("S" & RandomNumberGeneratedCounter + 1).Value = EmployeeArray(RandomEmployeeNumberPickedArray.Keys()(RandomNumberGeneratedCounter + 97), 1)
Next ' Loop Back
End Sub