Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Determining a Random Value.

Determining a Random Value

by Allen Wyatt
(last updated August 16, 2014)

3

VBA provides a function to return a random value. You wouldn't necessarily use this function by itself, but as part of a larger macro that may require the use of random values. The syntax for the function is as follows:

x = Rnd()

where x is the result. The value returned will always be between 0 and 1. To translate this to some other random value, all you need to do is multiply the result by the highest number you want to consider. For instance, if you wanted a random integer number between 1 and 25, you could use the following code line:

x = Int(25 * Rnd()) + 1

Since Rnd always returns a value between 0 and 1 (but never 1 itself), multiplying what it returns by 25 and then using the Int function on that result will return a number between 0 and 24. Finally, 1 is added to this result, so that x will be equal to a number between 1 and 25, inclusive.

If you want a random floating point number, then simply removing the Int function from this code will not work correctly. Instead, you'll need to modify the formula in this manner:

x = (Rnd() * 24) + 1

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9751) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Determining a Random Value.

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

Closing All Open Workbooks

Excel provides a handy (but little-known) shortcut for closing all the workbooks you have open. This tip explains how easy it ...

Discover More

Maintaining Formatting when Inserting Documents

Word allows you to easily insert the contents of one document into another. Doing so, however, may result in unintended ...

Discover More

Deleting a Hyperlink

Hyperlinks can be helpful in some worksheets but bothersome in others. Here's how to get rid of any hyperlinks you don't ...

Discover More

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!

More ExcelTips (ribbon)

Using a Macro to Select a Modified Table Body

One of the big challenges when processing your data using macros is to make sure that you select all the data (and just the ...

Discover More

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Specifying a Delimiter when Saving a CSV File in a Macro

You can, within a macro, save a workbook in several different file formats that are understood by Excel. However, you may not ...

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}] 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 7 + 6?

2014-08-19 10:23:13

Bryan

@JJ Olinchak: The Excel function you are thinking of is RANDBETWEEN (not randOMbetween). There is no VBA equivalent, but you can roll your own or use Application.WorksheetFunction.RandBetween(low, high).


2014-08-18 01:23:17

Geir Njaa

The RND() function is not truly random unless you initialize the random number generator with RANDOMIZE first.

If you do not RANDOMIZE, RND will produce the same sequence of numbers every time you restart Excel.


2014-08-17 10:03:39

JJ Olinchak

Excel also has "RandomBetween" function. To generate an integer between 1 and 25, type: =RandomBetween(1,25)


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.