Hi All,
I wanted to put together a quick spreadsheet for my family to quickly assign pollyanna names (Christmas gift-giving event, for those unfamiliar). Obviously, everyone in the pool should receive a gift and we do not want people in the same nuclear family (i.e. siblings) to be assigned to one another, and each name should also not be assigned to itself. I have the first names in column A, and last names in Column B, and I would like to place the first name of the person for which the gift should be bought in column E.
So far, I was able to throw together this simple code (after some googling)to randomly generate a match that is not in one's nuclear family, and is not one's self:
Sub randm()
Dim sh As Worksheet, nr As Long
Static AlreadyRandomized As Boolean
If Not AlreadyRandomized Then
AlreadyRandomized = True
Randomize
End If
Set sh = Sheets(1) 'Edit sheet name
For i = 2 To 29
Do
nr = Int((29 - 2 + 1) * Rnd + 2)
Loop While sh.Range("B" & i).Value = sh.Range("B" & nr).Value
sh.Range("E" & i) = sh.Range("A" & nr)
Next i
End Sub
However, for the life of me, I can't figure out how to make the code check the randomly generated name (A & nr) against the range of previously generated names, and re-generate a new name if found to be a duplicate.
Anyone have a suggestion?
I wanted to put together a quick spreadsheet for my family to quickly assign pollyanna names (Christmas gift-giving event, for those unfamiliar). Obviously, everyone in the pool should receive a gift and we do not want people in the same nuclear family (i.e. siblings) to be assigned to one another, and each name should also not be assigned to itself. I have the first names in column A, and last names in Column B, and I would like to place the first name of the person for which the gift should be bought in column E.
So far, I was able to throw together this simple code (after some googling)to randomly generate a match that is not in one's nuclear family, and is not one's self:
Sub randm()
Dim sh As Worksheet, nr As Long
Static AlreadyRandomized As Boolean
If Not AlreadyRandomized Then
AlreadyRandomized = True
Randomize
End If
Set sh = Sheets(1) 'Edit sheet name
For i = 2 To 29
Do
nr = Int((29 - 2 + 1) * Rnd + 2)
Loop While sh.Range("B" & i).Value = sh.Range("B" & nr).Value
sh.Range("E" & i) = sh.Range("A" & nr)
Next i
End Sub
However, for the life of me, I can't figure out how to make the code check the randomly generated name (A & nr) against the range of previously generated names, and re-generate a new name if found to be a duplicate.
Anyone have a suggestion?