Lucky Draw VBA with multiple winners, multiple conditions
Hello Guys,
So what I've been struggling with for a few days is trying to find / adapt / create a template of a lucky winner draw vba code that can:
1. randomly draw a number of values (winners) from column A - without having duplicates
2. for each of the drawn winners also check if the info associated to them in column B is also unique
3. print winners and corresponding info from column B to spreadsheet
I've managed to find a guy that wrote a code that does point 1 but I have no idea how to integrate the second match and validation into the entire thing. I've attached an example sheet with what I am looking to do.
Re: Lucky Draw VBA with multiple winners, multiple conditions
Thank you for your suggestion but it's still not what I'm looking for. I need the script to also check for duplicate codes, for example:
If Person1 sent Code003 but Person4 also sent Code003, if the script first picks Person1 as a winner it needs to not be able to then pick Person4 because that persons code would already have been picked before.
Re: Lucky Draw VBA with multiple winners, multiple conditions
Try:
PHP Code:
Sub PickNamesAtRandom() Dim i&, rng, r, name As String, code As String, dicName As Object, dicCode As Object Set dicName = CreateObject("Scripting.Dictionary") Set dicCode = CreateObject("Scripting.Dictionary") rng = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value For i = 1 To UBound(rng) If Not dicName.exists(rng(i, 1)) Then dicName.Add rng(i, 1), "" If Not dicCode.exists(rng(i, 2)) Then dicCode.Add rng(i, 2), "" Next Randomize For i = 1 To Range("D3").Value Do r = Int(Rnd * UBound(rng)) + 1 name = rng(r, 1): code = rng(r, 2) Loop Until dicName.exists(name) And dicCode.exists(code) dicName.Remove (name): dicCode.Remove (code) Range("D5").Offset(i, 0).Value = name Next End Sub
Bookmarks