Excel/combin() function

Advertisement


Question
QUESTION: I am using the combin() function. I am looking for a way to display the listings and not just the output of the formula. e.g. combin(5,2) = 10 possible combinations. I would like to be able to pout put the 10 values. 1&2, 1&3, 1&4, 1&5, 2&3,2&4,2&5, 3&4,3&5,4&5 are the combinations.

I tried a few VBA samples from the internet, but can't seem to get them working.

ANSWER: Put the 5 items in column F,
Run macro comb8:

Dim CurrentRow

Sub Comb8()
   lmt = Application.CountA(Range("F:F"))
   [a:a].ClearContents
   [a1].Select
   '''Generate combinations of 8 items taken 1 thru 8 at a time
   For i = 1 To lmt
       'Single apostrophe surrounded by quotes
       Comb lmt, i, 1, "'"
   Next
   Application.Goto Range("A1"), True
   For i = 1 To lmt
       [a:a].Replace i, Cells(i, 6).Value
   Next
   'remove items which aren't a pair:
   For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
       If Len(Cells(i, 1).Value) - Len(Application.Substitute(Cells(i, 1).Value, " ", "")) <> 2 Then Cells(i, 1).ClearContents
   Next
   Columns(1).Sort key1:=[a1], order1:=xlAscending, Header:=xlNo
End Sub

'''Generate combinations of integers k..n taken m at a time, recursively
Sub Comb(ByVal n As Integer, ByVal m As Integer, _
        ByVal k As Integer, ByVal s As String)
   If m > n - k + 1 Then Exit Sub
   If m = 0 Then
       ActiveCell = s
       ActiveCell.Offset(1, 0).Select
       Exit Sub
   End If
   Comb n, m - 1, k + 1, s & k & " "
   Comb n, m, k + 1, s
End Sub




---------- FOLLOW-UP ----------

QUESTION: That worked for combin(5,2). Not sure how it knows the number of items in each combination. e.g combin(5,4) retruns 5 values. It would be nice if it could work off of a Private Function where the 2 values of (5,4) for example were input and then the macro launched displaying the 5 items. Any combination is possible for this application of course.

Answer
Put the number of items in each combination in cel G1. Run this:

Sub Comb8()
   lmt = Application.CountA(Range("F:F"))
   [a:a].ClearContents
   [a1].Select
   '''Generate combinations of 8 items taken 1 thru 8 at a time
   For i = 1 To lmt
       'Single apostrophe surrounded by quotes
       Comb lmt, i, 1, "'"
   Next
   Application.Goto Range("A1"), True
   For i = 1 To lmt
       [a:a].Replace i, Cells(i, 6).Value
   Next
   'remove items which aren't a pair:
   For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
       If Len(Cells(i, 1).Value) - Len(Application.Substitute(Cells(i, 1).Value, " ", "")) <> Range("G1").Value Then Cells(i, 1).ClearContents
   Next
   Columns(1).Sort key1:=[a1], order1:=xlAscending, Header:=xlNo
End Sub

'''Generate combinations of integers k..n taken m at a time, recursively
Sub Comb(ByVal n As Integer, ByVal m As Integer, _
        ByVal k As Integer, ByVal s As String)
   If m > n - k + 1 Then Exit Sub
   If m = 0 Then
       ActiveCell = s
       ActiveCell.Offset(1, 0).Select
       Exit Sub
   End If
   Comb n, m - 1, k + 1, s & k & " "
   Comb n, m, k + 1, s
End Sub  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.