Playing with a Full Deck

Written by Allen Wyatt (last updated January 17, 2026)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


1

Excel is great at generating random numbers, but it is less great at filling a range of cells with random numbers in which no number is repeated twice. For instance, you might want to populate 52 cells with the numbers 1 through 52, in random order. (This is very similar to choosing cards from a deck in random order, where a particular card can only be chosen once. Thus, the title for this tip.)

If you are using Excel 2021 or Excel 365, then the solution is very simple using this formula:

=SORTBY(SEQUENCE(52), RANDARRAY(52))

As long as there is nothing in the 52 cells beneath where you use the formula, you are golden. The SEQUENCE function returns the values 1 through 52, the RANDARRAY returns an array of 52 random values, and SORTBY uses those random values to sort the SEQUENCE. Easy peasy.

If you are using an older version of Excel, then there is no combination of functions that will allow you to get the desired results without the use of helper columns. The easiest solution is to work with macros. The following macro will do the trick nicely:

Sub FillRand()
    Dim X As Long
    Dim RandIndex As Long
    Dim Nums As Variant
    Dim c As Range

    Randomize
    Nums = Evaluate("ROW(1:" & Selection.Count & ")")
    X = UBound(Nums)
    For Each c In Selection
        RandIndex = Int(X * Rnd + 1)
        c.Value = Nums(RandIndex, 1)
        Nums(RandIndex, 1) = Nums(X, 1)
        X = X - 1
    Next c
End Sub

To use the macro, start by selecting the cells you want to have filled with sequential values in a random order. When you run the macro, that range is filled. For instance, if you select ten cells and then run the macro, then those cells are filled with the numbers 1 through 10, in random order.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8269) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

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

Unhiding Multiple Worksheets

You can hide a bunch of worksheets at the same time, but Excel makes it impossible to unhide a bunch at once. You can, ...

Discover More

Doubling Your Money

Make your money last longer by using your head when printing labels. Here's a great example of how you can double the ...

Discover More

Enforcing a Do-Not-Use Word List

Got a list of words you don't want to appear in your documents? There are a number of ways that you can make sure they ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Checking the Scope of a Defined Name

When creating a macro to work with the names of defined ranges, you may need to know whether the scope of the name is for ...

Discover More

Editing Macros

Even if you do nothing but record macros, sooner or later you will have a need to edit what you record. Here's how to get ...

Discover More

Inserting a Page Break Every X Rows

As you format your worksheet, Excel allows you to add page breaks where you'd like. If you want to put in a series of ...

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}] (all 7 characters, in the sequence shown) 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 7?

2026-01-17 07:06:18

Mike J

Probably a little off topic, but the stipulation that 52 cells needed to be pre-selected prompted this suggestion.The macro selects down any number of cells as input via MsgBox prior to running this type of sub that requires a range to be pre-selected.

Could easily be modified to include other criteria.

Sub SelectCellsDownFromSelection()
    Dim n As Variant
    Dim startRange As Range
    Dim resultRange As Range
    Dim cell As Range
    Dim hasData As Boolean
    Dim c As Range
    Dim response As VbMsgBoxResult

    ' Ensure something is selected
    If TypeName(Selection) <> "Range" Then
        MsgBox "Please select one or more cells first.", vbExclamation
        Exit Sub
    End If

    Set startRange = Selection
    ' Ensure selection is on a single row - REM out if not required
    If startRange.Rows.Count > 1 Then
        MsgBox "Please select cells on a single row only.", vbExclamation
        Exit Sub
    End If

    ' Ask for number of cells down
    n = Application.InputBox( _
            Prompt:="Enter the number of cells to select downward:", _
            Title:="Select Cells Down", _
            Type:=1)

    ' Handle Cancel or invalid input
    If n = False Or n < 1 Or n <> CLng(n) Then
        MsgBox "Please enter a valid whole number greater than zero.", vbExclamation
        Exit Sub
    End If

    ' Build the resulting range
    For Each cell In startRange.Cells
        If resultRange Is Nothing Then
            Set resultRange = cell.Resize(n, 1)
        Else
            Set resultRange = Union(resultRange, cell.Resize(n, 1))
        End If
    Next cell

    ' Check for existing data
    hasData = False
    For Each c In resultRange.Cells
        If Not IsEmpty(c.Value) Then
            hasData = True
            Exit For
        End If
    Next c

    ' Prompt user if data exists - REM out if overwrite is OK
    If hasData Then
        response = MsgBox( _
            "Some cells in the target range already contain data." & vbCrLf & _
            "Do you want to overwrite them?", _
            vbYesNo + vbExclamation, _
            "Confirm Overwrite")

        If response = vbNo Then Exit Sub
    End If
    ' Select the final range
    resultRange.Select
End Sub


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.