Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to generate random character strings in a range in Excel?

Sometimes you may need to generate random strings in cells, such as different passwords. This article tries to show you some tricks to generate different random strings in Excel.

Generate random character strings with formulas

Generate random character strings with VBA code

Easily generate random character strings with Kutools for Excel


Easily generate random character strings in selected range:

It is easy to generate random character strings with specific characters and text length with the Insert Random Data utility of Kutools for Excel. Download the full feature 60-day free trail of Kutools for Excel now!

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!

Generate random character strings with formulas

The following formulas can help you generate random numbers, letters and alphanumeric values in a range in Excel.

1. To create a random 5-digit number between 10000 and 99999, use this formula: =RANDBETWEEN(10000,99999), and press Enter key, a 5-digit number will be display in a cell, then select the cell and drag the fill handle across to the range that you want to contain this formula, a range of 5-digit numbers have been generated, see screenshots:

doc-generate-strings1 -2 doc-generate-strings2

Note: you can change the arguments to get your need.

2. To create a random letter, use the this formula: =CHAR(RANDBETWEEN(65,90)). This formula can generate a random letter from a to z, if you need to create four random letters, you need to use & character to add the letters. Such as =CHAR(RANDBETWEEN(65,90))& CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(65,90)). Then you will get a four random letters, and then drag the fill handle over to the range that you want to contain this formula. See screenshot:

doc-generate-strings3

Notes:

(1.)The formula is valid in Excel 2007, 2010, and 2013, but not Excel 2003. In Excel 2003, please use the formula =CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65))

(2.) In formula 65 is A and 90 is Z.

(3.) You can use the character & to add the number of letters you need.

3. To create a random alphanumeric string that has two letters and two numbers, use this formula: =CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(10,99) And you will get the following strings in a range which contain two letters and two numbers:

doc-generate-strings4

Note: you can change the arguments to get the digit number as you need, and use & to add the number of letters.


Generate random character strings with VBA code

If you feel above formulas are difficult and troublesome, the following VBA code can help you much easier. Please do as this:

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following macro in the Module window.

Public Function RandomizeF(Num1 As Integer, Num2 As Integer)
'Update 20131107
Dim Rand As String
Application.Volatile
getLen = Int((Num2 + 1 - Num1) * Rnd + Num1)
Do
    i = i + 1
    Randomize
    Rand = Rand & Chr(Int((85) * Rnd + 38))
Loop Until i = getLen
RandomizeF = Rand
End Function

3. Then save and close the code, in a cell, enter this function =RandomizeF(x,y) to insert a random character string with a minimum length of x characters, and a maximum length of y characters.

4. In this example, I will use function =RandomizeF(5,10) to generate a character string which between 5 and 10 characters. Then press Enter key, select the cell and drag the fill handle to the range you want to contain this function. And random of alphanumeric and specific character strings which between 5 and 10 characters have been created. See screenshot:

doc-generate-strings5


Generate random character strings with Kutools for Excel

Is there a way to generate random strings with letters, numbers, and special characters, or more? Kutools for Excel's Insert Random Data is an excellent random number (and text string) generator, which can generate random numbers, random text string, or random numbers & text& symbols with all kinds of characters, including letters, numbers, special characters, space, and even custom strings.

Kutools for Excel, with more than 120 handy functions, makes your jobs easier. 

1. Select a range in which you will generate random strings, and then click Kutools > Insert > Insert Random Data. See screenshot:

3. In the Insert Random Data dialog box, click String tab, and choose the type of characters as you need, then specify the length of the string in the String length box, and finally click the OK button. See screenshot:

Then the selected range has been filled with random character strings.

Tip: if you want to generate or insert a specified formatting data strings (such as ????@.??.com) in a range, you can also use this utility. Do as this:

1. Select a range and specify the characters and check By mask. Then input the specified data strings you need. See screenshot:

Note: Using ? to indicate a digit of random character in the final specified formatting strings.

2. Then click OK or Apply. The specified formatting data strings have been generated randomly as follows. See screenshot:

Kutools for Excel's Insert Random Data can help you easily process the following operations in a range cells.

  • Generate or insert random numbers in a range
  • Generate or insert random date in a range
  • Generate or insert a custom list in a range

Tip.If you want to have a free trial of this utility, please go to download the software freely first, and then go to apply the operation according above steps.


Office Tab - Tabbed Browsing, Editing, and Managing of Workbooks in Excel:

Office Tab brings the tabbed interface as seen in web browsers such as Google Chrome, Internet Explorer new versions and Firefox to Microsoft Excel. It will be a time-saving tool and irreplaceble in your work. See below demo:

Click for free trial of Office Tab!

Office Tab for Excel



Related Article:



Recommended Productivity Tools

Office Tab

gold star1 Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

Kutools for Excel

gold star1Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

gold star1300 New Features for Excel, Make Excel Much Easy and Powerful:

  • Merge Cell/Rows/Columns without Losing Data.
  • Combine and Consolidate Multiple Sheets and Workbooks.
  • Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
  • Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
  • More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Screen shot of Kutools for Excel

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Crystal · 5 years ago
    VBA method worked great. Thanks so much. I really needed this!
    Take care...
  • To post as a guest, your comment is unpublished.
    Dan88 · 5 years ago
    I was wondering if there was a way to add a generic word to the beginning of each generated code, i.e. DISCOUNT56979, DISCOUNT88531, etc. The reason I ask is so that I can track discounts used from different leads.
    • To post as a guest, your comment is unpublished.
      HelloKelly · 5 years ago
      In the Insert Random Data dialog box, there is a [b]By mask[/b] option on the String tab that can help you.

      For example, Add the “DISCOUNT” before the 5 random characters, please check the [b]By mask[/b] option, and then enter the “[i][b]DISCOUNT?????[/b][/i]” in the following box.
      • To post as a guest, your comment is unpublished.
        RRK · 3 years ago
        Try This: ="DISCOUNT"&RANDBETWEEN(10000,99999)
        This Code is for 5 Random numeric character after "Discount"

        For alphanumeric try this:
        ="DISCOUNT"&CHAR(RANDBETWEEN(65,90))& CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(0,9)

        or

        ="DISCOUNT"&CHAR(RANDBETWEEN(65,90))& CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(10,99)
  • To post as a guest, your comment is unpublished.
    JD99 · 5 years ago
    Hi Guys,
    I would like to exclude zeros, O's and X's from the randomly generated string. How is best to achieve this? Many thanks
    • To post as a guest, your comment is unpublished.
      HelloKelly · 5 years ago
      Hi,
      It is impossible to exclude some characters from the random string when they are generating with the methods in this article.

      However, you can replace the excluded characters with other letters or numbers in the random strings.

      First of all select these strings, and then open the Find and Replace dialog box with hotkeys of Ctrl + F, at last replace each excluded character with a specified letter or number.
  • To post as a guest, your comment is unpublished.
    HappyE · 5 years ago
    Thanks for your sharing. For generate random alphanumeric function is not working in Excel 2003 version. Also, how to make it to 8 digits instead of 4?
    • To post as a guest, your comment is unpublished.
      HelloKelly · 5 years ago
      Hi HappyE,
      To generate 8 digits of random alphanumeric, you can repeat the & CHAR(INT(RAND() * 25 + 65)) 8 times in the function. That’s meaning the function changes to:

      =CHAR(INT(RAND( )* 25 + 65)) & CHAR(INT(RAND() * 25 + 65)) & CHAR(INT(RAND() * 25 + 65)) & CHAR(INT(RAND() * 25 + 65))& CHAR(INT(RAND() * 25 + 65)) & CHAR(INT(RAND() * 25 + 65)) & CHAR(INT(RAND() * 25 + 65))& CHAR(INT(RAND() * 25 + 65))
      • To post as a guest, your comment is unpublished.
        MayZ · 4 years ago
        HelloKelly,
        Great formula, thanks for posting it as I've found it very useful generating passwords. Do you know how to generate it without repeating characters at all? Example - it just returned "JUUJFENQ" but I can't have the repeating 'UU'.
        TIA,
        MayZ
  • To post as a guest, your comment is unpublished.
    HelloKelly · 5 years ago
    Hi LAurent,

    Thank you for your comment. I am very sorry the first method with formula (=CHAR) works well in Excel 2007 and 2010, but not Excel 2003. In Excel 2003, you can try the formula

    =CHAR(INT(RAND()* 25 + 65))
    or
    =CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65)) & CHAR(INT(RAND()* 25 + 65))

    By the way, the VBA code works fine in Excel 2003, and we recommend you try it.