Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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: Listing Combinations.

Listing Combinations

Written by Allen Wyatt (last updated June 29, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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
    Next

    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
        Next
        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
        Next
        i = i + 1
    Loop
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
    Next
    Finished = Temp
End Function

Function FindFirstSmall(n, r, i)
    j = r
    Do Until Cells(i, j).Value <> j + (n - r)
        j = j - 1
    Loop
    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)
    Loop
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
    Next
    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
    Loop
    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.

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 (11891) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel 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. ...

MORE FROM ALLEN

Peculiar Font Differences

Have you noticed page layout differences when you open a document on different systems? There are a number of reasons why ...

Discover More

Adding Automatic Lines

Want an easy way to add lines in your document? You can do it by making sure Word is using one of its AutoFormat features.

Discover More

Replacing Dashes with Periods

Replacing one character in a text value with another character is easy. All you need to do is use the SUBSTITUTE ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Using a Numeric Portion of a Cell in a Formula

If you have a mixture of numbers and letters in a cell, you may be looking for a way to access and use the numeric ...

Discover More

Looking Backward through a Data Table

Sometimes you need to look backward, through the information above your formula, to find the data you need. This can be ...

Discover More

Returning the Minimum of Integers of a Range

If you have a range of numbers that contain both integers and decimal numbers, you may have a need to determine the ...

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 nine minus 5?

2019-07-08 07:04:44

Graham

Hi Allen, I'm attempting to list out combinations for a set of numbers, such as 11, 12, 13, 14, 15, 16 in 4-digit combinations (eg 12 13 14 15; 11 13 15 16;). I've tried the second set of macros and have not figured out a way to separate the numbers in a single cell for excel to recognise the 2 characters a whole - 1 integer (eg 12 not 1 AND THEN 2). Is this doable by adjusting the second set of macros above or can this be accomplished at all with excel? Thank you. Graham


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.