Randomly pick a name only once

rigg_14

Board Regular
Joined
Dec 8, 2002
Messages
64
I have a list of 50 names and need to pick 10 of those names at random. Each name can be picked only once. Can you please help me out with the code for this. I have experimented with =Rand() with no luck and =INDEX($A$1:$A$50,RANK(C1,$C$1:$C$10)) with no luck. I have also tried =INDEX(A:A,RANDBETWEEN(1,COUNTA(A:A))). I must be missing something.
All names are in A1:A20 and I would like to display the 10 randomly picked names in C1:C10.

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this

Code:
Sub RandomNames()
Dim R As Range, S As Range
Dim Count As Long, X As Long, Y As Long, Found As Integer
Dim xName As String
Dim Names() As String
Dim RandName As Integer
Set R = Sheets("Names").Range("A2")
Set S = Sheets("RandomNames").Range("A2")
Count = Range(R, R.Offset(65000, 0).End(xlUp)).Count
ReDim Names(Count)
Do Until X = 10
  RandName = Application.WorksheetFunction.RandBetween(1, 50)
  xName = R.Offset(RandName - 1, 0)
  Found = 0
  For Y = 1 To X
    If Names(Y) = xName Then
      Found = 1
      Exit For
    End If
  Next Y
  If Found = 0 Then
    X = X + 1
    Names(X) = xName
    S.Offset(X - 1, 0) = Names(X)
  End If
Loop
  
End Sub
 
Upvote 0
Due you have the excel add
-in more func? there's a function in there Mrand which will do exactly what you need
 
Upvote 0
With C1 containing a column label or left blank, try...

C2, confirmed with CONTROL+SHIFT+ENTER, and copied down:

=INDEX($A$1:$A$20,SMALL(IF(ISNA(MATCH($A$1:$A$20,$C$1:C1,0)),ROW($A$1:$A$20)-ROW($A$1)+1),INT(RAND()*(ROWS($A$1:$A$20)-(ROW()-ROW($C$2))))+1))
 
Upvote 0
Domenic I have entered the formula in cell c2 confirmed it with ctrl+shift+enter and then tried to drag the formula down. The {} do stay in, but the result is #N/A, any ideas. Cell C2 does give me a random name but the rest do not.
 
Upvote 0
Yes cell c2 has

{=INDEX($A$1:$A$20,SMALL(IF(ISNA(MATCH($A$1:$A$20,$C1:C1,0)),ROW($A1:$A$20)-ROW($A$1)+1),INT(RAND()*(ROWS($A$1:$A$20)-(ROW()-ROW($C2))))+1))}

c3 has

{=INDEX($A$1:$A$20,SMALL(IF(ISNA(MATCH($A$1:$A$20,$C2:C2,0)),ROW($A2:$A$20)-ROW($A$1)+1),INT(RAND()*(ROWS($A$1:$A$20)-(ROW()-ROW($C3))))+1))}

and so on.
 
Upvote 0
You've missed some absolute references...

=INDEX($A$1:$A$20,SMALL(IF(ISNA(MATCH($A$1:$A$20,$C$1:C1,0)),ROW($A$1:$A$20)-ROW($A$1)+1),INT(RAND()*(ROWS($A$1:$A$20)-(ROW()-ROW($C$2))))+1))
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,272
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top