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.
Written by Allen Wyatt (last updated April 26, 2025)
This tip applies to Excel 97, 2000, 2002, and 2003
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
Randomize
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.
Note:
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.
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!
Random values are often needed when working with certain types of data. When you need to generate a random value in a ...
Discover MoreIt is not unusual to need to select two random items from a list. There are a couple of ways you can approach the task; ...
Discover MoreExcel provides several different functions that you can use to generate random numbers. One of the most useful is the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2026-01-24 08:59:01
Barbie
If the headings from which you generate your TOC are also bold, there is another workaround.
Set the TOC styles to be Not Bold. View the TOC field code, and remove the \h switch if it is there (it only appears in later versions of Word.)
Set the Heading styles to be Not Bold, and then manually apply Bold formatting to each heading (you can use Find & Replace to do this, and for future use could even record a macro to apply the Heading style and Bold formatting in one step).
Now, the explicitly-applied formatting will carry over to the TOC. (It will also carry over any other formatting you explicitly apply to your headings, so be careful.
2026-01-24 08:58:54
Diana Donovan
Thank you for your answer about styling Word TOC page numbers differently from the TOC paragraph style itself. Especially thanks for saving me from trying to use character styles for those TOC page numbers. Your answer definitely works!
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments