Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Generating Unique, Sequential Names.

Generating Unique, Sequential Names

Written by Allen Wyatt (last updated October 20, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Steven is testing some software and he needs to feed into the program a bunch of "fake" names. He would like these names to be patterned such as Nameaaa, Nameaab, Nameaac, and so on through Namezzz. This would require creating 17,576 names (26 x 26 x 26). He wonders if there is an easy way to generate all these names in Excel.

This sort of repetitive task just cries out for a macro. (They are great for doing boring, dull, repetitive tasks that you don't want to do manually.) Here is a simple macro that can do the required grunt work:

Sub CreateNames()
    Dim i As Integer
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer

    i = 1
    For x = 97 To 122
        For y = 97 To 122
          For z = 97 To 122
              Cells(i, 1) = "Name" & Chr(x) _
                & Chr(y) & Chr(z)
              i = i + 1
            Next
        Next
    Next
End Sub

The macro uses three counter variables (x, y, and z) to serve as "counter variables" that control which letter of the alphabet is appended to the "name" stuffed into a cell. Notice that the For ... Next loops range from 97 to 122, which are the ASCII codes for lowercase a through z.

If you don't want to use a macro for some reason, type the following formula into cell A1 of a blank worksheet:

="Name" & CHAR((ROW()-1)/676+97)&CHAR(MOD(
(ROW()-1)/26,26)+97)&CHAR(MOD(ROW()-1,26)+97)

This is a single formula, and it results in "Nameaaa" being displayed. Copy the formula down through row 17,576 and you'll have your fake names.

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 (12128) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Generating Unique, Sequential Names.

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

Automatically Formatting Text within Quotes

Some people use quote marks around text to make it stand out. At some point you may want to treat the quoted text ...

Discover More

Printing a Single Column in Multiple Columns

Ever printed out a worksheet only to find that you have text only at the left side of each page? You can use more of each ...

Discover More

Formatting Axis Patterns

Create a chart in Excel and you can then modify it almost any way you desire. One modification is to adjust the color or ...

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 (menu)

Using SUM In a Macro

Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.

Discover More

Running a Macro When a Worksheet is Deactivated

You can easily configure Excel so that it runs a specific macro whenever a worksheet is deactivated. Just follow the easy ...

Discover More

Getting User Input in a Dialog Box

Want to get some input from the users of your workbooks? You can do it by using the InputBox function in a macro.

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 two more than 7?

2018-10-20 11:27:56

Rick Rothstein

Actually, since we are working with fixed ranges, we can use the square bracket shorthand notation for the Evaluate function and shorten up that code line slightly...

'========START CODE========
Sub CreateNames()

Range("A1:A17576") = [IF({1},"Name"&CHAR((ROW(A1:A17576)-1)/676+97)&CHAR(MOD((ROW(A1:A17576)-1)/26,26)+97)&CHAR(MOD(ROW(A1:A17576)-1,26)+97))]

End Sub'========END CODE========


2018-10-20 11:22:30

Rick Rothstein

Since you know of a formula that will work, why not use it in the macro and eliminate all of that looping? The following one-liner (albeit a rather long one) macro will produce the same output as your macro does...

'========START CODE========
Sub CreateNames()

Range("A1:A17576") = Evaluate("IF({1},""Name""&CHAR((ROW(A1:A17576)-1)/676+97)&CHAR(MOD((ROW(A1:A17576)-1)/26,26)+97)&CHAR(MOD(ROW(A1:A17576)-1,26)+97))")

End Sub
'========END CODE========

Note: If you only want to create a partial list, say 5000 names instead of the entire list of 17576 total names, simply change the range you are assigning to from Range(*A1:A17576") to Range("A1:A5000")... nothing else has to change.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.