Excel/combin() function


EXCEL 2003

I have a difficult problem using the combin function that I can't seem to find a solution for online.

I have daily performances of 10 different trading strategies and I want to display and rank the top performing combination of 4 of them every day. (So in excel this amounts to 210 different combinations per day using the function COMBIN(10,4).

The issue I am having is that Im not sure how to display the various combinations as output, which I can then rank and use for statistical interpretation.


On the active sheet,
Put your trading symbols in A1:J1   (10 symbols)

then run this macro:

Sub Combinations()
 Dim n As Integer, m As Integer
 Dim v As Variant, rng As Range
 numcomb = 0
 Set rng = Range("A1:J1")
 'Set rng = rng.Resize(1, 5)
 v = Application.Transpose(Application _
 n = UBound(v, 1)
' m = InputBox("Taken how many at a time?", "Combinations")
 m = 4
 If Application.Combin(n, m) > 64530 Then
   MsgBox "Too many to write out, quitting"
   Exit Sub
 End If
 Comb2 n, m, 1, "'", v
End Sub

'Generate combinations of integers k..n taken m at a time, recursively
Sub Comb2(ByVal n As Integer, ByVal m As Integer, _
 ByVal k As Integer, ByVal s As String, v As Variant)
 Dim v1 As Variant
 If m > n - k + 1 Then Exit Sub
 If m = 0 Then
   'Debug.Print "->" & s & "<-"
   v1 = Split(Replace(Trim(s), "'", ""), " ")
   For i = LBound(v1) To UBound(v1)
     ActiveCell.Offset(0, i) = v(v1(i))
   ActiveCell.Offset(1, 0).Select
   Exit Sub
 End If
 Comb2 n, m - 1, k + 1, s & k & " ", v
 Comb2 n, m, k + 1, s, v
End Sub

This will generate the 210 Unique combinations starting in row 3, column A

Tom Ogilvy

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


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 About.com. All rights reserved.