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: Listing Combinations.

Listing Combinations

by Allen Wyatt
(last updated May 9, 2016)


Ron knows he can use the COMBIN function to determine the number of combinations that can be made from a number of digits. He's wondering, however, if there is a way to list out all the combinations themselves.

There is no built-in way to list combinations in Excel. You can, however, create a macro to do the listing for you. If you want to find the unique combinations in a set of sequential numbers starting at 1, then the following set of macros will do the trick. All you need to do is run the function TestCNR and you will end up with a "matrix" of cells that represent the number of 4-digit combinations in the sequential set of values ranging from 1 to 10.

Sub TestCNR()
    Cnr 10, 4
End Sub
Sub Cnr(n, r)
    i = 1
    For j = 1 To r
        Cells(i, j).Value = j

    Do Until Finished(n, r, i)
        j = FindFirstSmall(n, r, i)
        For k = 1 To j – 1
            Cells(i + 1, k).Value = Cells(i, k).Value
        Cells(i + 1, j).Value = Cells(i, j).Value + 1
        For k = j + 1 To r
            Cells(i + 1, k).Value = Cells(i + 1, k - 1).Value + 1
        i = i + 1
End Sub
Function Finished(n, r, i)
    Temp = True

    For j = r To 1 Step -1
        If Cells(i, j).Value <> j + (n - r) Then
            Temp = False
        End If
    Finished = Temp
End Function
Function FindFirstSmall(n, r, i)
    j = r
    Do Until Cells(i, j).Value <> j + (n - r)
        j = j - 1
    FindFirstSmall = j
End Function

The macro overwrites whatever is in your worksheet, so make sure you run the test with a blank worksheet displayed. If you want to change the size of the set or the number of elements in the subset, just change the values passed in the TestCNR routine.

If you want to pull unique combinations from a string of characters (for instance, the letters of the alphabet), then you need to use a different set of macros. The following will work fine; it assumes that the characters you want to use as your "universe" is in cell A1 and the number you want in each unique combination is in cell A2.

Sub FindSets()
    Dim iA() As Integer
    Dim sUniv As String
    Dim iWanted As Integer
    Dim j As Integer
    Dim k As Integer

    sUniv = Cells(1, 1).Value
    iWanted = Cells(2, 1).Value

    ReDim iA(iWanted)
    For j = 1 To iWanted
        iA(j) = j
    Next j

    iRow = PutRow(iA, sUniv, 1)

    Do Until DoneYet(iA, Len(sUniv))
        j = WorkHere(iA, Len(sUniv))
        iA(j) = iA(j) + 1
        For k = j + 1 To iWanted
            iA(k) = iA(k - 1) + 1
        Next k
        iRow = PutRow(iA, sUniv, iRow)
End Sub
Function DoneYet(iB, n) As Boolean
    iMax = UBound(iB)
    Temp = True
    For j = iMax To 1 Step -1
        If iB(j) <> j + (n - iMax) Then
            Temp = False
        End If
    DoneYet = Temp
End Function
Function WorkHere(iB, n) As Integer
    iMax = UBound(iB)
    j = iMax
    Do Until iB(j) <> j + (n - iMax)
        j = j - 1
    WorkHere = j
End Function
Function PutRow(iB, sUniv, i)
    iMax = UBound(iB)
    sTemp = ""
    For j = 1 To iMax
        sTemp = sTemp & Mid(sUniv, iB(j), 1)
    Next j
    Cells(i, 2).Value = sTemp
    PutRow = i + 1
End Function

Run the FindSets macro and the different combinations desired end up in column 2. Be careful when running the macro, however. The number of combinations can get very large very quickly. For instance, if you put 26 letters (A through Z) in cell A1 and the value 5 in cell A2, the macro will crash. Why? Because there are 65,780 possible five-character combinations and only 65,536 rows in which to place them.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6766) 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: Listing Combinations.

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. ...


Using the GotoButton Field

Need to jump from one place in your document to another? One way to do this is through the user of the GotoButton field, ...

Discover More

Getting User Input in a Dialog Box

Want to grab some interactive input from a user in your macro? The best way to do that is with the InputBox function, ...

Discover More

Deleting a Chart

Charts serve a purpose, and sometimes that purpose is temporary. If you want to get rid of a chart, here's how to do it.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

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

Shortcut for Viewing Formulas

If you need to switch between viewing formulas and viewing the results of those formulas, you'll love the keyboard ...

Discover More

Summing Absolute Values

You can easily sum a series of values in Excel, but it is not so easy to sum the absolute values of each value in a ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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 1 + 5?

2017-02-02 14:24:35

Carlos Santiago

What's the word on the error I get at: "For k = 1 To j – 1"

2016-11-03 11:33:30


I would like to create a comma delimited string of names in cell A0...
Dim arrWsNames() As String
arrWsNames = Split(Cells(1, 1).Value, ",")

and then pass this array to your routine Cnr ->
Cnr arrWsNames, 2

I would then like to generate the matrix of name combinations from the array... are you able to show me how please...?
Thank you in advance.

2016-08-04 09:36:41


I get an error: "For k = 1 To j – 1" does anybody knows what I can do?

2015-07-22 17:06:50


If I have a table with 4 rows and 5 columns to select a set of 4, how many combinations will I have if I must select only one item from each row but can select one or more from a column. See table below

A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3
A4 B4 C4 D4 E4

2014-12-11 06:02:48


I get an error: "For k = 1 To j – 1" does anybody knows what I can do?

2014-05-03 08:51:00



I Have copied the code in Sub Testcnr() to a module and tried to run it. The debugger gives me variable i "not Defined" What do I need to do to get this code to run.

Thanks in advance for your assistance

2014-03-26 23:45:50


how do I continue from one worksheet to the next if there is not enough rows to complete the task

2013-06-03 15:07:50

Marie Vasco


let's say we want to use this but the combinations are going to go past excel's limit of over 1,000,000 rows. How can we modify this macro so that we can "spill" the combinations to another sheet in the same excel workbook after excel reaches it limit in the sheet that it is in?

I greatly appreciate your help :)


2013-03-12 05:02:56


could you show me how to list out all the 13-digits combination?

2013-02-10 12:26:28


Ok, so in the line (Line 2) that reads:

Cnr 10, 4

If I change that to:

Cnr 12, 9

I would get a list of all the 9-digit combinations from the set of numbers 1,2,3,4,5,6,7,8,9,10,11 and 12? Right?


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

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.