Selecting a Specific Number of Random Names from a List Based on Values in a Cell

Ambiversion

New Member
Joined
Jul 13, 2016
Messages
6
I'm throwing together a simple spreadsheet which will contain a list of names populating column B. The purpose of the spreadsheet is to randomly select a number of names from Column B and list them in Column D. In C2, I have =COUNTA(B:B) which gives me the total number of names listed in Column B. Below that in C3, I will have the number of names that month that need to be pulled randomly, which is 50% of the names listed in Column B divided by 12. To do this, I have =(C2*0.5)/12. In D4, I'm hoping to be able to list the number of names found in C3 (which will be based on the number of names in the list counted in C2) at random.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Someone has given me this formula which can be used if I know the maximum number of names that could be pulled randomly in Column D =IF(ROW()<=$C$2,INDEX($B:$B,RANDBETWEEN(1,COUNTA($B:$B)),1),"") This works EXCEPT that it should produce 4 names in Column D where that formula has been entered if 4 is the number in C3, but it is only producing 1 name. If I change C3 to 5, then it will produce 2 names, 6 will produce 3, etc. So the math must be wrong here somewhere. Any ideas?
 
Upvote 0
I'm throwing together a simple spreadsheet which will contain a list of names populating column B. The purpose of the spreadsheet is to randomly select a number of names from Column B and list them in Column D. In C2, I have =COUNTA(B:B) which gives me the total number of names listed in Column B. Below that in C3, I will have the number of names that month that need to be pulled randomly, which is 50% of the names listed in Column B divided by 12. To do this, I have =(C2*0.5)/12. In D4, I'm hoping to be able to list the number of names found in C3 (which will be based on the number of names in the list counted in C2) at random.

Try This
Code:
Option Explicit
Sub Random_Name()


Dim Names_Array() As Variant, Names_Needed As Long, Random_Number, Name_Count As Long, Index_Array() As Long, _
Final_Names_Array() As Variant, X1 As Long, Last_Row As Long, ws As Worksheet
Set ws = ActiveSheet


Last_Row = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row


Names_Needed = (Last_Row * 0.5) / 12 ' the number of names that month that need to be pulled randomly




Names_Array = ActiveSheet.Range("B2:B" & Last_Row).Value 'assumes data starts in row 2
                                                                                                       
ReDim Index_Array(1 To Names_Needed, 1 To 1)            'Will hold index numbers


ReDim Final_Names_Array(1 To Names_Needed, 1 To 1)


Name_Count = 1
Do While Name_Count <> Names_Needed  'While the Final array is not full


    Random_Number = ((Names_Needed - 1 + 1) * Rnd + 1)  'Random number between 1 and the number of names that need to be pulled for that month
    
    If IsInArray(Random_Number, Index_Array) = False Then 'No repeat names
        
        Index_Array(Name_Count, 1) = Random_Number
        
        Final_Names_Array(Name_Count, 1) = Names_Array(Index_Array(Name_Count, 1), 1)
        
        Name_Count = Name_Count + 1
    
    End If


Loop


ws.Range("D:D" & Names_Count + 1).Value = Final_Names_Array 'ACCOUNTING FOR HEADERS (+1)


End Sub


Public Function IsInArray(valToBeFound As Variant, ARR As Variant) As Boolean
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: Function to check if a value is in an array of values
'INPUT: Pass the function a value to search for and an array of values of any data type.
'OUTPUT: True if is in array, false otherwise
Dim element As Variant
On Error GoTo IsInArrayError: 'array is empty
    For Each element In ARR
        If element = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next element
Exit Function
IsInArrayError:
On Error GoTo 0
IsInArray = False
End Function
 
Upvote 0
Change Last_Row = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
to
Last_Row = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
 
Upvote 0
Put your data and formulas as shown below:

<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:76.04px;" /><col style="width:76.04px;" /><col style="width:78.89px;" /><col style="width:76.04px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td><td >D</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td > </td><td >name1</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td > </td><td >name2</td><td style="text-align:right; ">79</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td > </td><td >name3</td><td style="text-align:right; ">4</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td > </td><td >name4</td><td > </td><td >name26</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td > </td><td >name5</td><td > </td><td >name74</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td > </td><td >name6</td><td > </td><td >name54</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td > </td><td >name7</td><td > </td><td >name52</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td > </td><td >name8</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td > </td><td >name9</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td > </td><td >name10</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td > </td><td >name11</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td > </td><td >name12</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td > </td><td >name13</td><td > </td><td > </td></tr></table><br /><table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#00ff00;background-color:#fffcf9; color:#000000; "><tr><td ><b></b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Formula</td></tr><tr><td >C2</td><td >=COUNTA(B:B)</td></tr><tr><td >C3</td><td >=ROUNDUP((C2*0.5)/12,0)</td></tr></table></td></tr></table>

---

If you use the RANDBETWEEN formula, every time you change to cell the names will change.
The most convenient is to execute a macro so that the names are fixed.

Code:
Sub List_Random_Names()
    Dim n As New Collection, j As Long, i As Long, num As Variant
    
    j = 4
    Range("D4:D" & Rows.Count).Clear
    For i = 1 To [C2]
        n.Add i
    Next
    For i = 1 To [C3]
        num = WorksheetFunction.RandBetween(1, [C2])
        Cells(j, "D") = Cells(num, "B")
        n.Remove num
        j = j + 1
    Next
End Sub

INSERT A MODULE
Press Alt-F11 to open the VBA editor. From the menu select Insert > Module. On the sheet that opens, paste the code previous.
Close the editor (press Alt-Q). From Excel, press Alt-F8 to open the macro selector, and select List_Random_Names and press Run.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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