random non-repeating names

  • Thread starter Thread starter rivet
  • Start date Start date
R

rivet

I have a sheet of names (all ID'd 1-700), and I would like to randomly
pull 35 names from the list with out any repeats. Where should I begin.
 
Assuming your IDs are in col A and the names are in col B, this sub
will pick 35 unique names and put them in col C.

Sub GetNames()
Dim x As Integer
Dim intNum As Integer
Dim arrNames(34) As String, strName As String
Dim blnNameFound As Boolean

x = 0
Do Until x = 35

blnNameFound = False
Randomize (Timer)

'--Randomly pick the ID
intNum = Rnd(1) * 700 + 1

'--Get the corresponding name
strName = Cells(intNum, 2)

'--Check the array for that name and set a boolean to true if found
For y = 0 To 34
If strName = arrNames(y) Then
blnNameFound = True
Exit For
End If
Next y

'--If the name was not found, add it to the array
If blnNameFound = False Then
arrNames(x) = strName
x = x + 1
End If

Loop

Does this help?


'Put the 35 names in column C
For x = 0 To 34
Cells(x + 1, 3) = arrNames(x)
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top