Hi everyone, I asked for some help the other day and I got some wonderful help from some of you, thank you.
I asked for some code to pick some names from a list, and to pick each name, but remove it from a list.
The code works well, but it brings up a messagebox in the middle of the screen. Is there any way the code can be changed so that the messagebox saying "Name:" comes up below the blue box (i.e. somewhere in cells C10:F20?
The code and a screenshot is below. Thank you
I asked for some code to pick some names from a list, and to pick each name, but remove it from a list.
The code works well, but it brings up a messagebox in the middle of the screen. Is there any way the code can be changed so that the messagebox saying "Name:" comes up below the blue box (i.e. somewhere in cells C10:F20?
The code and a screenshot is below. Thank you
VBA Code:
Sub winners3()
Dim a As Variant, b As Variant, arr As Variant, iRow As Variant
Dim dic As Object
Dim nNames&, i&, j&, k&, m&, x&, y&, z&
With Range("A2", Range("A" & Rows.Count).End(3))
.Interior.Color = xlNone
a = .Value
Range("H2:H" & Rows.Count).ClearContents
End With
ReDim b(1 To UBound(a), 1 To 1)
Set dic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(a)
If Not dic.exists(a(i, 1)) Then
k = k + 1
b(k, 1) = a(i, 1)
End If
dic(a(i, 1)) = dic(a(i, 1)) & i + 1 & ","
Next
nNames = Application.InputBox("How many names should be picked", "Random name picker", Type:=1)
If nNames = 0 Then
MsgBox "Cancelled"
Exit Sub
End If
If nNames > dic.Count Then
MsgBox "The requested number of names is greater than the number of available names"
Exit Sub
End If
Randomize
j = 2
arr = Evaluate("ROW(1:" & dic.Count & ")") 'total records
For z = 1 To nNames 'how many do i want
x = Int(Rnd * k + z)
y = arr(z, 1)
arr(z, 1) = arr(x, 1)
arr(x, 1) = y
k = k - 1
m = arr(z, 1) 'random number
Range("H" & j).Value = b(m, 1)
For Each iRow In Split(dic(b(m, 1)), ",")
If iRow <> "" Then Range("A" & iRow).Interior.Color = vbRed
Next
MsgBox "Name: " & b(m, 1), vbOKOnly
j = j + 1
Next
MsgBox "Congratulations to all our winners"
End Sub