Excel.Tips.Net ExcelTips (Menu Interface)

Playing with a Full Deck

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Playing with a Full Deck.

How's that for a tip title? The title refers to the fact that you may have a need to populate a range of cells with a series of numbers in random order. For instance, you might want to populate 52 cells with the numbers 1 through 52, in random order. (This would be similar to drawing cards from a shuffled deck, thus the tip title.)

There obviously is no built-in Excel function to provide this capability, so you are left to work with macros. Fortunately, such a macro is not terribly difficult to create. The following macro will do the trick nicely:

Sub FillRand()
    Dim nums() As Integer
    Dim maxval As Integer
    Dim nrows As Integer, ncols As Integer
    Dim j As Integer, k As Integer
    Dim Ptr As Integer

    Set s = Selection
    maxval = s.Cells.Count
    nrows = s.Rows.Count
    ncols = s.Columns.Count

    ReDim nums(maxval, 2)

    'Fill the initial array
    For j = 1 To maxval
        nums(j, 1) = j
        nums(j, 2) = Int((Rnd * maxval) + 1)
    Next j

    'Sort the array based on the random numbers
    For j = 1 To maxval - 1
        Ptr = j
        For k = j + 1 To maxval
            If nums(Ptr, 2) > nums(k, 2) Then Ptr = k
        Next k
        If Ptr <> j Then
            k = nums(Ptr, 1)
            nums(Ptr, 1) = nums(j, 1)
            nums(j, 1) = k
            k = nums(Ptr, 2)
            nums(Ptr, 2) = nums(j, 2)
            nums(j, 2) = k
        End If
    Next j

    'Fill in the cells
    Ptr = 0
    For j = 1 To nrows
        For k = 1 To ncols
            Ptr = Ptr + 1
            s.Cells(j, k) = nums(Ptr, 1)
        Next k
    Next j
End Sub

This macro uses a two-dimensional array (nums) to figure out which numbers to use and the order in which they should be used. Near the beginning of the macro the array is filled with a static number (1 through the number of cells) and a random number between 1 and the number of cells. This second number is then used to sort the array. Once the array is stored, it is a simple matter to place the original numbers in the cells.

By the way, the reason a two-dimensional array is used is because the Rnd function that VBA uses to generate random numbers can return duplicate values. Thus, even through the second dimension of the array can have duplicates in it, when the array is finally sorted, the first dimension will not have duplicates.

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2280) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Playing with a Full Deck.

Related Tips:

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

Greg    10 Jan 2016, 12:16
A simpler method without using macros is to populate one column with the number 1 thru 52 then in an adjacent column use the RND function to put a random number against each row.

Sort both the columns using the 'random' column as the key and you've randomised the order.

As in the example above you could end up with the same random number in the sort key but that does not matter.
Rick Rothstein    09 Jan 2016, 10:43
I should have mentioned in both of my previous messages that the code I posted works in XL2003 (the earliest version I own) to XL2010 (the lastest version I own)... I see no reason why the code would not work in XL2013 or later, but I have no idea if it will work in versions prior to XL2003.
Rick Rothstein    09 Jan 2016, 10:35
I constructed the code in my previous message just before going to sleep... when I awoke just now, it occurred to me that I could reduce the code to a single loop... excluding the Dim statement, my modified macro (below) is now down to eight active lines of code.

Sub FillRand()
  Dim X As Long, RandIndex As Long, Nums As Variant, Cell As Range
  Nums = Evaluate("TRANSPOSE(ROW(1:" & Selection.Count & "))")
  For Each Cell In Selection
    RandIndex = Int((UBound(Nums) - X) * Rnd + 1)
    Cell.Value = Nums(RandIndex)
    Nums(RandIndex) = Nums(UBound(Nums) - X)
    X = X + 1
End Sub
Rick Rothstein    09 Jan 2016, 06:11
Here is another (more concise) way to write the macro...

Sub FillRand()
  Dim X As Long, RandIndex As Long
  Dim Temp As Variant, Nums As Variant
  Dim Cell As Range
  Nums = Evaluate("TRANSPOSE(ROW(1:" & Selection.Count & "))")
  For X = UBound(Nums) To 1 Step -1
    RandIndex = Int(X * Rnd + 1)
    Temp = Nums(RandIndex)
    Nums(RandIndex) = Nums(X)
    Nums(X) = Temp
  For Each Cell In Selection
    X = X + 1
    Cell.Value = Nums(X)
End Sub

Also note that the selection does not have to be contiguous so long as the parts that make it up do not overlap in any way.

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2017 Sharon Parq Associates, Inc.