Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Selecting Random Names.

Selecting Random Names

by Allen Wyatt
(last updated June 30, 2017)

5

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 your recalculate, the list of extracted names is regenerated.

Another potential approach requires the use of multiple columns. Simply follow these steps:

  1. Enter =RAND() in cell B1.
  2. Enter the following formula in cell C1:
  3.      =RANK(B1,$B$1:$B$1000)
    
  4. Select the range B1:C1, and fill down to row 1000.
  5. Select the range B1:C1000.
  6. Press Ctrl+C to copy the range to the Clipboard.
  7. Display the Home tab of the ribbon.
  8. Click the down-arrow under the Paste tool and then select Paste Special. Excel displays the Paste Special dialog box. (See Figure 1.)
  9. Figure 1. The Paste Special dialog box.

  10. Make sure the Values radio button is selected.
  11. Click on OK. You now have static values in B1:C1000, which means they won't change every time the worksheet is recalculated.
  12. Select a cell in column C.
  13. Display the Data tab of the ribbon.
  14. Click the Sort tool. Excel display the Sort dialog box. (See Figure 2.)
  15. Figure 2. The Sort dialog box.

  16. Click on OK. The table (range A1:C1000) is sorted according to the values in column C.

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 iRows As Integer
    Dim iCols As Integer
    Dim iBegRow As Integer
    Dim iBegCol As Integer
    Dim J As Integer
    Dim sCells As String
    
    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

To use this 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12475) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Selecting Random Names.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing Multiple Envelopes

Need to print envelopes quite often? An easy way to do it is to create an envelope template, as described in this tip.

Discover More

Flipping a Drawing Object

Place a drawing object in your document, and it doesn't have to stay that way. You can flip a drawing object in either of two ...

Discover More

Getting Rid of MRU Entries

By default Word dutifully keeps track of the different files you've worked on in the past. If you want to delete an entry ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Colors in an IF Function

You can use the IF worksheet function to test for a number of different conditions or values. You can't use it to check for ...

Discover More

Returning Blanks with VLOOKUP

Normally the VLOOKUP function returns a value, and if it can't return a value it returns a zero. Here's how you can use the ...

Discover More

Calculating the Day of the Year

Need to know what day of the year a certain date is? You can figure it out easily using the formulas in this tip.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 9?

2016-06-18 15:42:12

Trevor

@Willy Vanhaelen

agree with your point regarding undefined variables.

See my original response on an easy way to get a reference to the Forms Library


2016-06-18 08:34:18

Willy Vanhaelen

@Dave Imhoff

Again bad macro code in this tip !

First of all there are 2 variable that are not declared:
Dim iWantRow As Integer
Dim tempDO As Variant

But most important for "Set tempDO = New DataObject" to work you must add a reference to the "Microsoft Forms 2.0 Object Library".

To do so, click on "Tools" in the VBA Editor menu and select "References...".
In the dialog that is shown click on "Browse"
Then look for "FM20.DLL", select it and click "Open"
Check this reference and click OK.

Now try to run the macro again.


2016-06-17 16:41:47

Dave Imhoff

I tried this in Excel 2007 and it errors at New DataObject with a message User-defined type not defined.


2016-05-10 14:53:36

Devanand

Hi,

Am new to excel macros, need you help.

Need a vba code to select 5 random values, For example

5 tax tickets, 5 salary tickets, etc. from the Sheet2 that contains more salary and tax tickets

Please help..

Thank You,


2013-02-16 06:47:19

Trevor Shuttleworth

Being a programmer at heart, I like the VBA solution. However, it didn't work "out of the box"

With some research and testing, I found:

It needs a reference to the Microsoft Forms 2.0 Object Library
One way to get that is to add a form
(even though you don't need to use it
and you can delete the form immediately.
The reference to the library will stay.)

Regards, Trevor


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.