Share via

Random Name with Multiple Entries

Anonymous
2015-05-01T00:21:18+00:00

Hello -

I would like to have a list of names that could have multiple entries into a raffle of sorts, and use excel to choose a name from these entries at random.  I have seen several posts with macros written but am not familiar enough with how these work to use them myself.   

Example

Joe - 5 entires

Alex - 15 entries

Karen - 2 entries

Susan - 1 entry

Bob - 7 entries

How can I make excel count Joe 5 times, Alex 15 times and Susan only once?

Thank you in advance for your help.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

OssieMac 48,001 Reputation points Volunteer Moderator
2015-05-02T00:00:10+00:00

I have uploaded an example workbook to the following link on One Drive

Link removed. Link in post of January 16, 2016 is still valid. Code below is still valid for this answer.

You should be able to download the file, unzip and then insert your list of names with number of tickets into column B and C under the column headers.

Click the button to create a list. It will clear any existing list and prize winners and it creates a list of all the names repeated for the number of times shown in column C.

Click the Draw Raffle button and it will draw one name at a time and insert it against the prize. You can have as many prizes as you like; just add the prizes on the left. You might even want to edit 1st Prize, 2nd Prize etc with the name or description of the actual prize.

The code is designed to select a random row number from 2 to the bottom of the created list and correlates the row number to the name. (the row numbers are the ticket numbers). It cannot repeat a row number so no one gets multiple prizes with the same ticket but it will repeat the names up to the number of tickets for that name.

You can test the code in the workbook before using it with your real data. If you continue to click the Draw Raffle button then it will stop with a message after all tickets have been drawn.

The associated code is below but you can simply use the workbook that I uploaded.

You will need to enable macros in Options. See Help for how to do this. (The option to "Disable all with notification" should be OK.)

Sub CreateList()

    Dim rngNames As Range

    Dim rngCel As Range

    Dim rngDestin As Range

    Dim i As Long

    With Worksheets("Sheet1")

        .Range(.Cells(2, "E"), .Cells(.Rows.Count, "E").End(xlUp)).ClearContents

        .Range(.Cells(2, "I"), .Cells(.Rows.Count, "J")).ClearContents

        Set rngNames = .Range(.Cells(2, "B"), .Cells(.Rows.Count, "B").End(xlUp))

        .Cells(1, "E") = .Cells(1, "B")

        .Cells(1, "E").Font.Bold = True

        For Each rngCel In rngNames

            Set rngDestin = .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0)

            For i = 1 To rngCel.Offset(0, 1)

                rngDestin = rngCel.Value

                Set rngDestin = rngDestin.Offset(1, 0)

            Next i

        Next rngCel

        .Columns("E:E").AutoFit

    End With

End Sub

Sub DrawRaffle()

    Dim lngLastRow As Long

    Dim lngDrawRow As Long

    Dim strName As String

    With Worksheets("Sheet1")

        lngLastRow = .Cells(.Rows.Count, "E").End(xlUp).Row

        Do

            If WorksheetFunction.CountA(.Columns("E:E")) = WorksheetFunction.CountA(.Columns("I:I")) Then

                MsgBox "All names have been drawn."

                Exit Sub

            End If

            lngDrawRow = WorksheetFunction.RandBetween(2, lngLastRow)

            If WorksheetFunction.CountIf(.Columns("J:J"), lngDrawRow) = 0 Then

                Exit Do

            End If

        Loop

        .Cells(.Rows.Count, "I").End(xlUp).Offset(1, 0) = .Cells(lngDrawRow, "E").Value

        .Cells(.Rows.Count, "I").End(xlUp).Offset(0, 1) = lngDrawRow

        .Columns("I:J").AutoFit

    End With

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

30 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-02-01T02:13:06+00:00

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-05-04T07:03:18+00:00

    You may also be interested in formula based solution.

    As per snap shot, assuming your data is at A1:B5, row 1 being header then

    at E2 give this formula,

    =A2

    Then at E3 give this formula and copy it down.

    =IFERROR(IF(COUNTIF($E$2:E2,E2)<VLOOKUP(E2,$A$2:$B$5,2,0),E2,INDEX($A$2:$A$5,MATCH(E2,$A$2:$A$5,0)+1)),"")

    Ensure that there at least 1 entry to each guest, it should not be zero.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2015-05-03T15:03:55+00:00

    This is amazing and perfect!  I can't thank you enough!

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. OssieMac 48,001 Reputation points Volunteer Moderator
    2015-05-01T00:49:04+00:00

    Need to know how the worksheet is currently set up with the names and number of entries. Can you post a small screen shot of the data.

    If you are using win 2007 or later to create a screen shot:

    1. Search for the Snipping Tool application in Programs.
    2. Pin the application to the taskbar.
    3. To create the screen shot click the snipping tool icon in the Taskbar and then drag over the required area.
    4. Save the screen shot.
    5. To upload to this forum, click the "Insert Image" icon in the tool bar attached to the top your editing box, browse to where you saved the image and then click Insert button.

    I am thinking of a VBA code answer to create another worksheet with the names copied to the new worksheet a multiple number of times (as per the number of entries) and then use random numbers to select.

    For the above I also need to know is if the entries for each name are to be kept adjacent in the new list or should they be random in the new list.

    Was this answer helpful?

    0 comments No comments