Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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: Generating Random Testing Data.

Generating Random Testing Data

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


1

When you develop worksheets that will be used by other people, you should test those worksheets to make sure that they work as you expect. This is particularly true of worksheets that contain complex formulas or will be used for critical purposes. The concept of testing a worksheet means that you will need to generate some sort of data to use in testing the worksheet.

Entire books have been written on putting together testing suites for software. How rigorous you are in compiling test data depends, in large part, on the needs of your audience and the nature of your worksheet. Unfortunately, there is no quick cure-all that will automatically figure out what you need and generate the data for you. There are tools in Excel, however, that you can use toward this end.

First, if you need to generate random numeric values, you can use the RAND or RANDBETWEEN worksheet functions. The difference is that RAND generates a value between 0 and 1, and RANDBETWEEN generates integer values between any bounds you set. Once you generate the values, then use copy and "paste values" to flatten your data to the results provided by the functions. (If you don't, then RAND and RANDBETWEEN will generate new results every time you recalculate the worksheet.)

Random data may not be appropriate for your testing needs, however. This is particularly true when you are testing boundaries of your formulas. For instance, testing with large values, small values, or a combination of large and small values. Likewise, you may want to test for inappropriate values, such as using text as input rather than numbers (or vice versa). There are a whole contingent of conditions you need to think through, and then pick the type of data that is right for your needs.

If you prefer, you can use macros to generate testing data. The following macro fills a selected range of cells with a random numeric value, between whatever boundaries (minimum and maximum) that you set.

Sub RandNums()
    Dim MyRange As Range
    Dim lMin As Long, lMax As Long
    Dim dRand As Double

    ' If selection is not Excel Range
    If TypeName(Selection) <> "Range" Then Exit Sub

    Set MyRange = Selection

    ' Get Min and Max value
    lMin = CLng(InputBox("Minimum?"))
    lMax = CLng(InputBox("Maximum?"))

    Randomize
    Application.ScreenUpdating = False

    For Each c In MyRange.Cells
        ' Calculate random value, where
        ' Value >= Min And Value <= Max
        dRand = Rnd * (lMax - lMin) + lMin

        ' Use the following line only if the random
        ' value should be an integer
        ' dRand = Int(dRand)

        c.Value = dRand
    Next c

    Application.ScreenUpdating = True
End Sub

To use the macro, just select a range of cells that you want to contain random numeric values, and then run the macro. If you must use integer values in the cells, then you can "uncomment" the noted line within the macro.

If you want to fill a range of cells with random dates, then a slight modification to the RandNums macro results in the following.

Sub RandDates()
    Dim MyRange As Range
    Dim dtMin As Date, dtMax As Date
    Dim dtRand As Date

    ' If selection is not Excel Range
    If TypeName(Selection) <> "Range" Then Exit Sub

    Set MyRange = Selection

    ' Get Min and Max value
    ' From: 1/1/1990 (put your start Date)
    dtMin = #1/1/1990#
    ' To: Today
    dtMax = Date

    Randomize
    Application.ScreenUpdating = False

    For Each c In MyRange.Cells
        ' Calculate random value, where
        ' Value >= Min And Value <= Max
        dtRand = Rnd * (dtMax - dtMin) + dtMin
        dtRand = Int(dtRand)

        c.Value = dtRand
        ' Change format for cell, below, as desired
        c.NumberFormat = "m/d/yyyy"
    Next c

    Application.ScreenUpdating = True
End Sub

Again, just select a range and then run the macro. You can modify the initial values set to the dtMin and dtMax variables in order to specify the boundaries for the dates desired. You can also, if desired, change the formatting applied to the cells after the random date is stored within the cells.

If you want to generate a group of names for testing purposes, then you are into the realm of textual values. A great way to do this is to simply do a search on the Web for the phrase "random name generator." There are a good number of websites available where you can generate names for free.

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 (7989) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Generating Random Testing Data.

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

Preventing Straggling Heads

Undoubtedly you will want to format your document so that headings stay with the paragraph that follows the heading. ...

Discover More

Working with Elapsed Time

Work with times in a worksheet and you will eventually want to start working with elapsed times. Here's an explanation of ...

Discover More

Word's Native Measurement Unit

Word allows you to specify distances using a number of different measurement units. Figuring out how those measurement ...

Discover More

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!

More ExcelTips (ribbon)

Shortcut to Move between Two Worksheets

Moving between to adjacent worksheets is easy; Excel provides a shortcut key to do the trick. If you want to move between ...

Discover More

Disabled Page Setup Tools

It can be frustrating if you expect to use some of Excel's tools normally available on the ribbon, but those tools are ...

Discover More

Changing Your Name

One of the many pieces of information that Excel keeps track of is your name. If you want to change your name for Excel's ...

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 8 - 5?

2024-01-13 15:16:01

Dave Bonin

I always liked using a table of random numbers.

In particular, RAND Corp. published a table called 100,000 Random Normal Deviates (the title always makes me smile) in the 1950's. The table has been extensively tested and has no apparent statistical bias.

So why download and pick from a table of numbers?

1) Looking up random numbers (grab the next one in the table) is quick -- much quicker than generating random numbers.

2) Looking up random numbers (grab the next one in the table) is repeatable -- very important when you're trying to verify your formulas are correct.


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.