Written by Allen Wyatt (last updated October 28, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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.
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.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7989) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Generating Random Testing Data.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
If you like to use the mouse in your worksheet navigation efforts, you'll want to pay attention to this tip. Here you ...
Discover MoreWhen you use Excel with multiple workbooks open at the same time, you can use the ribbon tools to switch between ...
Discover MoreOne 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 MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-10-01 11:43:36
David Bonin
I strongly recommend against using the RAND() and RANDBETWEEN() worksheet functions.
Two reasons:
1) Every time the worksheet recalculates, all of the generated random values will change. That can make auditing your outputs against the inputs maddening.
2) These functions are slow. If you need to generate a lot (hundreds or thousands) of random numbers, then be prepared to wait a while.
The approach I took when creating a Monte-Carlo simulation program was to download a table titled "A Million Random Digits with 100,000 Normal Deviates". This table has been around since 1955 and it is well-vetted. You can download the table (all or just part of it) to a worksheet. Then just pick a place to start and use Excel to read values off the table.
Want a different batch of random values? Just pick a different place to start reading from.
Best thing is that your results are repeatable, which is very helpful for debugging.
2018-09-30 04:21:18
Hans
Generating random non-private data for testing purposes can really be a nuisance.
I was pointed to this site (https://www.fakenamegenerator.com/) and it really is an outcome
Free of charge and you can generate any type of lists you require.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments