Generating Random Strings

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


3

Richard has a need to generate random 15- and 20-character strings, and he thought he might be able to do it in Excel. The strings can contain letters and digits, with no spaces. He knows about the RANDBETWEEN function, but doesn't really see how it could be helpful for his purposes.

Actually, you can use the RANDBETWEEN function as the core of your formula. Start by putting into cell A1 all the possible characters that you may want included in your strings. For instance, you could put the following 62 characters in A1:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

Now, consider the following formula:

=MID(A1,RANDBETWEEN(1,LEN(A1)),1)

This uses the MID and RANDBETWEEN functions to select a single random character from whatever characters you placed in cell A1. Now, to lengthen what is returned, you just need to start combining this core formula using the ampersand character. For instance, the following will return 2 characters:

=MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)

If you want to return 15 characters, then you just need to use 15 iterations of the core formula:

=MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)&MID(A1,RANDBETWEEN(1,LEN(A1)),1)

This can (obviously) lead to some very long formulas. Fortunately, if you are using the version of Excel provided with Microsoft 365, you don't need to rely on super-long formulas to generate the strings you want. You can, instead, use the powerful RANDARRAY function combined with the CONCAT function.

You already have your base characters in cell A1. So, in cell A2 place either 15 or 20, depending on the length of string you want to generate. (Actually, you could put any other length in A2, but Richard specifically needed 15- and 20-character strings.) Now, use the following formula:

=CONCAT(MID(A1,RANDARRAY(1,A2,1,LEN(A1),TRUE),1))

The result is a random text string, just the length you want.

If you would rather use a macro, then you can easily create a user-defined function that will return a random-character string of any length you desire:

Function RandChars(iLen As Integer) As String
    Dim sSource As String
    Dim J As Integer
    Dim sTemp As String
    Dim AWF As WorksheetFunction

    Set AWF = Application.WorksheetFunction

    sSource = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    sSource = sSource & "abcdefghijklmnopqrstuvwxyz"
    sSource = sSource & "0123456789"

    sTemp = ""
    For J = 1 To iLen
        sTemp = sTemp & Mid(sSource,AWF.RandBetween(1, Len(sSource)), 1)
    Next J
    RandChars = sTemp
End Function

The characters you place into sSource are those that will be used to put together the random string. The macro utilizes the RANDBETWEEN worksheet function to pull the characters from sSource.

To use the macro, simply use a formula like this in your worksheet:

=RandChars(15)

The parameter you pass to the function determines the length of the string that is returned by the function.

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 (12823) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Opening a Workbook but Disabling Macros

Macros that run automatically when you open or close a workbook are quite helpful. You may not want them to run, however, ...

Discover More

Locking the Background Color

You can spend a lot of time getting the formatting in your worksheets just right. If you want to protect an element of ...

Discover More

Checking the Size of Apps

Need to know how much disk space is consumed by the small applications (apps) installed on your system? Windows makes it ...

Discover More

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!

More ExcelTips (ribbon)

Relative References to Cells in Other Workbooks

When you construct a formula and click on a cell in a different workbook, an absolute reference to that cell is placed in ...

Discover More

Combining Numbers and Text in a Cell

There are times when it can be beneficial to combine both numbers and text in the same cell. This can be easily done ...

Discover More

Summing Only Cells Containing Formulas

If you want to figure out the sum of all cells that contain formulas, there are a couple of ways you can go about it. ...

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 2 + 2?

2022-01-24 11:31:07

Mike D.

I love this, I never know about RANDBETWEEN. It could even help me create a random guessing game in excel that I have written before in Basic, Visual Basic and Arduino-C. I would love see if I can do it without VBA.

You wanted something between 15 to 20 characters so I put in an 'InputBox' in the code so you can enter the length of the random cypher. ‘iLen = InputBox("Enter length")’ Put this anywhere before the generator loop.
The only caveat is any re-calculation will bring up the Input Box, could become annoying.

To do this a different way I added another Cell reference, =RandChars(A3)
All you have to do is type in the length you want into cell A3 (or wherever you put the length seed), and after hitting enter it will automatically recalculate and give you the new random array in the length you asked for.
If you need another random array of the same length, you only need to recalculate (F9) the worksheet.


2022-01-23 19:48:17

Andy

Just following on from Jacques' suggestion, the new LET function is good for this as well, and keeps it contained within the single formula.

=LET(RandSourceString,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",CONCAT(MID(RandSourceString,RANDARRAY(1,RANDBETWEEN(15,20),1,LEN(RandSourceString),TRUE),1)))


2022-01-23 17:02:27

Jacques Raubenheimer

To get rid of the cell references, but still use the Excel365 formula listed above, define a name (I used RandSourceString) ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Then use the formula
=CONCAT(MID(RandSourceString,RANDARRAY(1,RANDBETWEEN(15,20),1,LEN(RandSourceString),TRUE),1))


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.