Written by Allen Wyatt (last updated October 6, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
A common task for many people is to pick a number of random names from a large list. For instance, you may be running a contest for your community, and you have 1,000 people that have entered. With their names in each row of a table, you may be wondering how to select a certain number of the names randomly.
As is often the case with Excel, there are a number of different approaches you can take. Each approach examined in this tip assumes that the names you need to select from are listed in cells A1 through A1000. Of course, your range of names could be shorter or longer, but the point is that they are in contiguous cells in column A. The examples also assume that you need to select 15 names at random from the list.
The first approach is to use the INDEX function. Enter the following formula in cells B1:B15:
=INDEX(A:A,INT((RAND()*1000)+1),1)
A similar formula uses the OFFSET function:
=OFFSET($A$1,ROUNDUP(RAND()*1000,0),0,1,1)
It is possible, but not probable, that you will get the same name twice in the resulting list. (The improbability comes because of the size of the original list. The larger the list, the less probable there will be duplicates in the extracted list.) If you do get a duplicate name, then simply force a recalculation of your worksheet by pressing F9. Each time you recalculate, the list of extracted names is regenerated.
Another potential approach requires the use of multiple columns. Simply follow these steps:
=RANK(B1,$B$1:$B$1000)
Figure 1. The Paste Special dialog box.
Figure 2. The Sort dialog box.
The result is that column C now contains a ranking of all the random numbers in column B. The first 15 rows contain your random names.
In this approach you could also have left out column C completely and simply sorted your list based on the static random values in column B. Again, the top 15 would be your random names.
Of course, there are any number of macro solutions you could use for this problem. The coding of any macro will be similar, relying on VBA's RND function to generate random numbers. Of all the possible macro solutions, perhaps the following is the most unique and offers some advantages not available with the workbook solutions discussed so far:
Sub GetRandom() Dim TempDO As Variant Dim iRows As Integer Dim iCols As Integer Dim iBegRow As Integer Dim iBegCol As Integer Dim sCells As String Dim J As Integer Dim iWantRow As Integer Set TempDO = New DataObject iRows = Selection.Rows.Count iCols = Selection.Columns.Count iBegRow = Selection.Row iBegCol = Selection.Column If iRows < 16 Or iCols > 1 Then MsgBox "Too few rows or too many columns" Else Randomize Timer sCells = "" For J = 1 To 15 iWantRow = Int(Rnd() * iRows) + iBegRow sCells = sCells & Cells(iWantRow, iBegCol) & vbCrLf Next J TempDO.SetText sCells TempDO.PutInClipboard End If End Sub
You should note that this macro defines—right after the variables are declared—a new DataObject and assigns it to the TempDO variable. If the macro bombs out on this line of code, it simply means that you need to tell VBA to reference the proper library:
Figure 3. The References dialog box.
In order to use the macro, just select the names from which you want to select the 15 random names. In the examples thus far, you would select the range A1:A1000. The macro then pulls 15 names at random from the cells, and puts them in the Clipboard. When you run the macro, you can then paste the contents of the Clipboard where ever you want. Every time the macro is run, a different group of 15 is selected.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12475) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Selecting Random Names.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Excel allows you to easily format cells with different fonts, borders, and colors. If you want to count the number of ...
Discover MoreEver want the IF function to only return a value if the condition it is testing is true, and not if the condition is ...
Discover MoreWhen performing a statistical analysis on a large dataset, you may want to use GEOMEAN to figure out the geometric mean ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-08-18 11:19:37
In the second example I don't see any purpose in using the RANK function nor do I see any reason to create static values for the random numbers. Just put =RAND() in B1:B1000 and sort column B. The first 15 (or any contiguous 15) cells in column A will be a random sample of 15. I call the technique "shuffling". I have used this for years and it certainly seems to work. If there is something wrong with it, I would be interested in your thoughts.
Column B will not appear to be sorted because the random numbers will be recalculated immediately after the sort.
If you want column A back in its original sequence, do an immediate UnDo or create a sequence column prior to sorting.
2018-08-18 05:11:13
Andy
Another method is to use the sampling tool which is part of the Analysis ToolPak.
There are instructions with screenshots at https://www.sageintelligence.com/tips-and-tricks/excel-tips-tricks/2016/02/sampling-analysis-tool/.
The article says it works for Excel 2007, 2010 and 2013 but I just tried it in Excel 365 and it worked fine.
It does require you to insert a new column next to the names and put numbers in that column. These could just be 1, 2, 3 ... . The add-in will then select the relevant numbers from that list.
You can then VLOOKUP to get the rest of the information from the selected rows if needed.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments