Hi there, I was assigned a task about an lucky draw event in our department as Xmas is coming. We have 42 employees and we have prepare 42 prizes as follows:
We also have the name list of 42 employees.
The arrangement:
1. We may draw an employee name first, then draw the prize for that employee; or other way round, draw the prize first, then draw the employee, who will then get that prize
2. We want the last drawn (i.e. the 42nd round) to be $500 cash prize. That is, 41 prizes (including 1 of the $500 cash prizes) to be randomly drawn in the first 41 rounds and the last round must be $500 cash prize
3. Two bosses are in the name list. If in some round, they get $500 cash prize, there should be a button for redraw. Or, it should be programmed that they can't get the top prize.
4. After 42 rounds are done, a Msgbox should pop up saying it's the End of Event.
Below is the current codes I tried. You don't have to read it if you already know how to build the arrangement above... Thanks!!
-------------------------------------------------------------------------------------
My current codes:
- There are two worksheets. One is "LuckyDraw", used for drawing; another is "List", used for processing and storage of data.
- Worksheets("List").Range(A1,A42) is the name list.
- I have made 2 text boxes (ActiveX control) which are used for showing the name and prize, and they are linked to cell R1 and S1 respectively. I have also made 2 form control buttons, which are used for drawing the name and prize respectively.
- However, since I am still quite new to VBA, I could not build the perfect codes as the arrangement above. Refer to the codes below, I created a Sub. If I run this Sub, the 42 employees will be randomly drawn immediately and stored in Range(B1,B42)
Then, I manually input the 42 cash prizes in advance in Range(C1:C42) (that is, basically cheating because in this case the prizes are not really randomly drawn. In each round, the Name will be drawn first, then the prize. After the prize is drawn, Column D will write 1. Worksheets("List").Cells(10, 10) is formulated =SUM(D1:D42), so that the program can identify which round it is now.
Please refer to the codes below:
For Name:
For Prize:
Problems of this code:
1. Have to draw name before prize; it could be boring
2. If the controller accidentally run the Name macro twice, it's messed up
3. If the controller accidentally run the prize first, it's messed up
4. Unable to eliminate the possibility of the two bosses getting the top prizes (if they are lucky enough)
Cash Prize | Total Number |
$500 | 2 |
$300 | 3 |
$200 | 5 |
$100 | 32 |
We also have the name list of 42 employees.
The arrangement:
1. We may draw an employee name first, then draw the prize for that employee; or other way round, draw the prize first, then draw the employee, who will then get that prize
2. We want the last drawn (i.e. the 42nd round) to be $500 cash prize. That is, 41 prizes (including 1 of the $500 cash prizes) to be randomly drawn in the first 41 rounds and the last round must be $500 cash prize
3. Two bosses are in the name list. If in some round, they get $500 cash prize, there should be a button for redraw. Or, it should be programmed that they can't get the top prize.
4. After 42 rounds are done, a Msgbox should pop up saying it's the End of Event.
Below is the current codes I tried. You don't have to read it if you already know how to build the arrangement above... Thanks!!
-------------------------------------------------------------------------------------
My current codes:
- There are two worksheets. One is "LuckyDraw", used for drawing; another is "List", used for processing and storage of data.
- Worksheets("List").Range(A1,A42) is the name list.
- I have made 2 text boxes (ActiveX control) which are used for showing the name and prize, and they are linked to cell R1 and S1 respectively. I have also made 2 form control buttons, which are used for drawing the name and prize respectively.
- However, since I am still quite new to VBA, I could not build the perfect codes as the arrangement above. Refer to the codes below, I created a Sub. If I run this Sub, the 42 employees will be randomly drawn immediately and stored in Range(B1,B42)
VBA Code:
Sub Begin()
Dim lastRow As Long
Dim winners As New Collection
Dim drawCount As Long
Dim nextWinner As Long
Dim testResult As Variant
On Error Resume Next
MsgBox "Lucky Draw Begins! Good Luck!", , "Lucky Draw Begins"
Worksheets("List").Columns(2).ClearContents
lastRow = Worksheets("List").Cells(Rows.Count, 1).End(xlUp).Row
For drawCount = 1 To 42
Do
nextWinner = Int(Rnd() * lastRow) + 1
Err.Clear
testResult = winners(Worksheets("List").Cells(nextWinner, 1).Value)
Loop Until Err.Number <> 0
winners.Add nextWinner, Worksheets("List").Cells(nextWinner, 1).Value
Worksheets("List").Cells(drawCount, 2).Value = Worksheets("List").Cells(nextWinner, 1).Value
Next drawCount
End Sub
Then, I manually input the 42 cash prizes in advance in Range(C1:C42) (that is, basically cheating because in this case the prizes are not really randomly drawn. In each round, the Name will be drawn first, then the prize. After the prize is drawn, Column D will write 1. Worksheets("List").Cells(10, 10) is formulated =SUM(D1:D42), so that the program can identify which round it is now.
Please refer to the codes below:
For Name:
Code:
Sub DrawName()
Dim NameRow As Long
On Error Resume Next
If Worksheets("List").Cells(10, 10) = 42 Then
MsgBox "End of draw. Thank you for your participation! Merry Christmas!!", , "This is the End"
Else: NameRow = Worksheets("List").Cells(10, 10) + 1
Worksheets("List").Cells(NameRow, 2).Copy Worksheets("LuckyDraw").Range("R1")
Worksheets("List").Cells(NameRow, 4) = "1"
End If
End Sub
For Prize:
Code:
Sub DrawGift()
Dim CouponRow As Long
On Error Resume Next
If Worksheets("List").Cells(10, 10) = 43 Then
MsgBox "End of draw. Thank you for your participation! Merry Christmas!!", , "This is the End"
Else: CouponRow = Worksheets("List").Cells(10, 10)
Worksheets("List").Cells(CouponRow, 3).Copy Worksheets("LuckyDraw").Range("S1")
MsgBox "Congratulations on winning " + Worksheets("List").Cells(CouponRow, 3).Value, , "Yeah!"
End If
Worksheets("LuckyDraw").Range("R1", "S1").ClearContents
End Sub
Problems of this code:
1. Have to draw name before prize; it could be boring
2. If the controller accidentally run the Name macro twice, it's messed up
3. If the controller accidentally run the prize first, it's messed up
4. Unable to eliminate the possibility of the two bosses getting the top prizes (if they are lucky enough)