+ Reply to Thread
Results 1 to 5 of 5

Creating a Raffle (Random Name Generator with Constraints)

Hybrid View

aleemullah Creating a Raffle (Random... 07-08-2019, 10:26 PM
AliGW Re: Creating a Raffle (Random... 07-09-2019, 02:09 AM
WideBoyDixon Re: Creating a Raffle (Random... 07-09-2019, 05:00 AM
aleemullah Re: Creating a Raffle (Random... 07-10-2019, 02:44 AM
WideBoyDixon Re: Creating a Raffle (Random... 07-10-2019, 05:11 AM
  1. #1
    Registered User
    Join Date
    08-10-2018
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    5

    Creating a Raffle (Random Name Generator with Constraints)

    Hi All,

    I need create a raffle sheet on excel whereby people interested in particular items (limited quantity) will be randomly selected. Will be running raffle against each available product (x the quantity available)

    See attached excel sheet for better visual representation of what I'm looking to create.

    Regards,
    Aleem
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,275

    Re: Creating a Raffle (Random Name Generator with Constraints)

    To have a button to do this will require VBA. I am moving your thread to that section of the forum.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Creating a Raffle (Random Name Generator with Constraints)

    OK. I created a macro to do the draw:

    Public Sub DrawAllRaffles()
    
    Dim lastRow As Long
    Dim thisRow As Long
    
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For thisRow = 3 To lastRow
        DrawRaffle thisRow
    Next thisRow
    
    End Sub
    Public Sub DrawRaffle(thisRow As Long)
    
    Dim quantity As Long
    Dim entrants As New Collection
    Dim thisCol As Long
    Dim lastCol As Long
    Dim winnerCol As Long
    Dim drawNext As Long
    Dim nextWinner As Long
    
    ' Get the quantity of prizes on offer
    quantity = Cells(thisRow, "B").Value
    
    ' Find the last column
    lastCol = Cells(2, Columns.Count).End(xlToLeft).Column
    
    ' Add in all the entrants and keep track of the first "Winner" column
    For thisCol = 3 To lastCol
        If StrComp(Left(Cells(2, thisCol).Value, 6), "winner", vbTextCompare) = 0 Then
            winnerCol = thisCol
            Exit For
        End If
        If Cells(thisRow, thisCol).Value = "X" Then entrants.Add Cells(2, thisCol).Value
    Next thisCol
    
    ' Randomize
    Randomize Now
    
    ' Fill in all the winner columns
    For drawNext = winnerCol To lastCol
        ' If we've run out of prizes or entrants then put in a blank value
        If (drawNext - winnerCol >= quantity) Or (entrants.Count < 1) Then
            Cells(thisRow, drawNext).Value = "-"
        Else
            ' Draw a random person and then remove them from the draw
            nextWinner = Int(Rnd(1) * entrants.Count) + 1
            Cells(thisRow, drawNext).Value = entrants(nextWinner)
            entrants.Remove nextWinner
        End If
    Next drawNext
    
    End Sub
    I then changed the buttons to be hyperlinks and trapped the follow event in the sheet:

    Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    
    DrawRaffle Target.Range.Row
    
    End Sub
    It seems to work as desired. Note that you can also run "DrawAllRaffles" to draw them all rather than one at a time.

    WBD
    Attached Files Attached Files
    Office 365 on Windows 11, looking for rep!

  4. #4
    Registered User
    Join Date
    08-10-2018
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    5

    Re: Creating a Raffle (Random Name Generator with Constraints)

    Hi WBD,

    This works perfectly. Thank you so much. You're a genius

    Regards,
    Aleem

  5. #5
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Creating a Raffle (Random Name Generator with Constraints)

    Quote Originally Posted by aleemullah View Post
    Hi WBD,

    This works perfectly. Thank you so much. You're a genius

    Regards,
    Aleem
    No problem and no argument

    WBD

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Weighted Random Number Generator for Raffle
    By nycraray6 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-09-2019, 05:06 AM
  2. Random Raffle with a Budget
    By Joe100 in forum Excel General
    Replies: 1
    Last Post: 02-16-2018, 02:06 AM
  3. Random Number Generator based on specific constraints
    By redstar15 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-19-2014, 03:49 PM
  4. Random Draw with constraints
    By Michael_B in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-22-2013, 07:32 PM
  5. Random number generation with constraints
    By sc11 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-14-2012, 12:41 AM
  6. Creating a skewed random number generator?
    By qweasd in forum Excel General
    Replies: 1
    Last Post: 07-20-2009, 11:44 AM
  7. Random Placement whilst fitting constraints
    By realmfighter in forum Excel General
    Replies: 2
    Last Post: 05-28-2007, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1