Selecting multiple random names from a list, with additional criteria

neilos81

New Member
Joined
Mar 8, 2022
Messages
14
Office Version
  1. 2021
Platform
  1. Windows
Hi all

I wonder if someone could help me. I'm not sure if what I want to do is possible using formulas in Excel but hoping someone can confirm either way.

I have a list of students and their relevant class. For simplicity class in column A and student name in column B. Classes would have codes but could be as simple as Class A through to Class L (And i can easily get a unique list of class codes). I'm wanting a random list of students but with the criteria that it is done by selecting 3 from each class to ensure a spread across all classes.

I've tried by assigning a random number to each student in the class and then using a vlookup for the class code and random number (1-3 for example) but I have the issue that I don't always get pupils assigned with the relevant random number. Is there a way to resolve this issue or is there a better way to do it from the start?

Many thanks in advance.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Rather than have us prepare a spreadsheet with data to show you how to do this, please provide a sample worksheet with several classes and enough students in each to allow for a solution. Use XL2BB to upload your sample so we don't have to recreate it. Help us to help you.
 
Upvote 0
Hi

Thank you. I did try to install the XL2BB when first posting but first had an issue with 'Protected View' and once I'd resolved that in the settings it now won't install due to having macros. I can't find a easy to allow it to install fully with macros. Can you advise?

Many thanks
 

Attachments

  • error message.jpg
    error message.jpg
    31.1 KB · Views: 6
Upvote 0
Book1.xlsx
ABCDEFG
1Class CodeStudentRandon Sample:Class AClass BClass C
2ClassAStudent1
3ClassAStudent2
4ClassAStudent3
5ClassAStudent4
6ClassAStudent5
7ClassAStudent6
8ClassAStudent7
9ClassAStudent8
10ClassAStudent9
11ClassAStudent10
12ClassAStudent11
13ClassAStudent12
14ClassAStudent13
15ClassAStudent14
16ClassAStudent15
17ClassBStudent1
18ClassBStudent2
19ClassBStudent3
20ClassBStudent4
21ClassBStudent5
22ClassBStudent6
23ClassBStudent7
24ClassBStudent8
25ClassBStudent9
26ClassBStudent10
27ClassBStudent11
28ClassBStudent12
29ClassBStudent13
30ClassBStudent14
31ClassBStudent15
32ClassCStudent1
33ClassCStudent2
34ClassCStudent3
35ClassCStudent4
36ClassCStudent5
37ClassCStudent6
38ClassCStudent7
39ClassCStudent8
40ClassCStudent9
41ClassCStudent10
42ClassCStudent11
43ClassCStudent12
44ClassCStudent13
45ClassCStudent14
46ClassCStudent15
Sheet1
 
Upvote 0
Table now shown above through XL2BB. In an ideal world, I'd like the 3 random names from each class to appear in the cells highlighted yellow.
 
Last edited:
Upvote 0
In column C =Rand()
Then bring the data into Power Query. The following Mcode is a bit long. Maybe someone else can see a shorter way. After random, cut and paste it to itself so that it doesn't keep refreshing and updating the PQ results.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Class Code"}, {{"Count", each _, type table [Class Code=text, Student=text, Random=number]}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Class Code", "Student", "Random"}, {"Class Code.1", "Student", "Random"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Count",{"Class Code"}),
    #"Split Column by Position" = Table.SplitColumn(#"Removed Columns", "Class Code.1", Splitter.SplitTextByRepeatedLengths(5), {"Class Code.1.1", "Class Code.1.2"}),
    #"Sorted Rows" = Table.Sort(#"Split Column by Position",{{"Random", Order.Ascending}, {"Student", Order.Ascending}, {"Class Code.1.2", Order.Descending}}),
    #"Merged Columns" = Table.CombineColumns(#"Sorted Rows",{"Class Code.1.1", "Class Code.1.2"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    #"Grouped Rows1" = Table.Group(#"Merged Columns", {"Merged"}, {{"Count", each _, type table [Merged=text, Student=nullable text, Random=nullable number]}}),
    #"Sorted Rows1" = Table.Sort(#"Grouped Rows1",{{"Merged", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows1", "Custom", each Table.AddIndexColumn([Count],"A",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Merged", "Student", "Random", "A"}, {"Merged.1", "Student", "Random", "A"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each [A] < 4),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Merged", "Student"})
in
    #"Removed Other Columns"
 
Upvote 0
I'm not sure if what I want to do is possible using formulas
Are you still using excel 2016 per your profile? If so, try the following. If you do have a newer version, a simpler formula may be available.
I have changed the student names as you had the same students in all 3 classes which I am assuming is not the case. :cool:
You also had a space in the class names in E1:G1 but not in column A so I have standardised that as well.
If you want to get a new list of names, just press F9.

24 09 03.xlsm
ABCDEFG
1Class CodeStudentRandon Sample:ClassAClassBClassC
2ClassAStudent1Student12Student19Student42
3ClassAStudent2Student11Student20Student33
4ClassAStudent3Student6Student28Student45
5ClassAStudent4
6ClassAStudent5
7ClassAStudent6
8ClassAStudent7
9ClassAStudent8
10ClassAStudent9
11ClassAStudent10
12ClassAStudent11
13ClassAStudent12
14ClassAStudent13
15ClassAStudent14
16ClassAStudent15
17ClassBStudent16
18ClassBStudent17
19ClassBStudent18
20ClassBStudent19
21ClassBStudent20
22ClassBStudent21
23ClassBStudent22
24ClassBStudent23
25ClassBStudent24
26ClassBStudent25
27ClassBStudent26
28ClassBStudent27
29ClassBStudent28
30ClassBStudent29
31ClassBStudent30
32ClassCStudent31
33ClassCStudent32
34ClassCStudent33
35ClassCStudent34
36ClassCStudent35
37ClassCStudent36
38ClassCStudent37
39ClassCStudent38
40ClassCStudent39
41ClassCStudent40
42ClassCStudent41
43ClassCStudent42
44ClassCStudent43
45ClassCStudent44
46ClassCStudent45
Random lists
Cell Formulas
RangeFormula
E2:G4E2=INDEX($B:$B,AGGREGATE(15,6,ROW($B$2:$B$46)/(($A$2:$A$46=E$1)*ISNA(MATCH($B$2:$B$46,E$1:E1,0))),RANDBETWEEN(1,COUNTIF($A$2:$A$46,E$1)-ROWS(E$1:E1)+1)))
 
Upvote 0
Are you still using excel 2016 per your profile? If so, try the following. If you do have a newer version, a simpler formula may be available.
I have changed the student names as you had the same students in all 3 classes which I am assuming is not the case. :cool:
You also had a space in the class names in E1:G1 but not in column A so I have standardised that as well.
If you want to get a new list of names, just press F9.

24 09 03.xlsm
ABCDEFG
1Class CodeStudentRandon Sample:ClassAClassBClassC
2ClassAStudent1Student12Student19Student42
3ClassAStudent2Student11Student20Student33
4ClassAStudent3Student6Student28Student45
5ClassAStudent4
6ClassAStudent5
7ClassAStudent6
8ClassAStudent7
9ClassAStudent8
10ClassAStudent9
11ClassAStudent10
12ClassAStudent11
13ClassAStudent12
14ClassAStudent13
15ClassAStudent14
16ClassAStudent15
17ClassBStudent16
18ClassBStudent17
19ClassBStudent18
20ClassBStudent19
21ClassBStudent20
22ClassBStudent21
23ClassBStudent22
24ClassBStudent23
25ClassBStudent24
26ClassBStudent25
27ClassBStudent26
28ClassBStudent27
29ClassBStudent28
30ClassBStudent29
31ClassBStudent30
32ClassCStudent31
33ClassCStudent32
34ClassCStudent33
35ClassCStudent34
36ClassCStudent35
37ClassCStudent36
38ClassCStudent37
39ClassCStudent38
40ClassCStudent39
41ClassCStudent40
42ClassCStudent41
43ClassCStudent42
44ClassCStudent43
45ClassCStudent44
46ClassCStudent45
Random lists
Cell Formulas
RangeFormula
E2:G4E2=INDEX($B:$B,AGGREGATE(15,6,ROW($B$2:$B$46)/(($A$2:$A$46=E$1)*ISNA(MATCH($B$2:$B$46,E$1:E1,0))),RANDBETWEEN(1,COUNTIF($A$2:$A$46,E$1)-ROWS(E$1:E1)+1)))
Thank you, looks super!! I'm currently on Excel 2021 but assume this will work still?
 
Upvote 0
I'm currently on Excel 2021
Might be worth updating your your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

assume this will work still?
Yes, it would still work but as I mentioned, a simpler formula might be available. Instead of copying that quite long formula down and across to all 9 cells you could use this much shorter formula and just copied across the top 3 cells.

24 09 03.xlsm
ABCDEFG
1Class CodeStudentRandon Sample:ClassAClassBClassC
2ClassAStudent1Student6Student24Student39
3ClassAStudent2Student10Student27Student36
4ClassAStudent3Student1Student17Student41
5ClassAStudent4
6ClassAStudent5
7ClassAStudent6
8ClassAStudent7
9ClassAStudent8
10ClassAStudent9
11ClassAStudent10
12ClassAStudent11
13ClassAStudent12
14ClassAStudent13
15ClassAStudent14
16ClassAStudent15
17ClassBStudent16
18ClassBStudent17
19ClassBStudent18
20ClassBStudent19
21ClassBStudent20
22ClassBStudent21
23ClassBStudent22
24ClassBStudent23
25ClassBStudent24
26ClassBStudent25
27ClassBStudent26
28ClassBStudent27
29ClassBStudent28
30ClassBStudent29
31ClassBStudent30
32ClassCStudent31
33ClassCStudent32
34ClassCStudent33
35ClassCStudent34
36ClassCStudent35
37ClassCStudent36
38ClassCStudent37
39ClassCStudent38
40ClassCStudent39
41ClassCStudent40
42ClassCStudent41
43ClassCStudent42
44ClassCStudent43
45ClassCStudent44
46ClassCStudent45
Random lists 2021
Cell Formulas
RangeFormula
E2:G4E2=LET(f,FILTER($B$2:$B$46,$A$2:$A$46=E1),INDEX(SORTBY(f,RANDARRAY(ROWS(f))),{1;2;3}))
Dynamic array formulas.
 
Upvote 0
Thank you so much, this is fantastic! Is there any simple way for me to understand what each part of the formula does? Always on the look out to educate myself where Excel is concerned...!
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,013
Latest member
Shubashish_Nandy

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