How Do I Get Excel To Generate All Possible Numerical Combinations?

anonymouspottamus

New Member
Joined
Oct 7, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. MacOS
I'm new to excel. I'm trying to generate all possible number combinations for the problem below:

Generate all numerical combinations when selecting a team of 5 people from a total of 20 candidates (range is 1-20), and 5 are being selected at a time
Rule 1: no repetition is allowed
Rule 2: order does not matter

Please note, the answer to =COMBIN(20,5) is not the complete answer to this question. The answer is supposed to result in a list of each of the 15.504 possible "team" combinations per my assignment. I don't know how to to use excel to generate that list.

Step-by-step guidance would really be appreciated.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
How about this?

VBA Code:
Sub GenerateCombinationsToSheet()
    Dim Candidates() As Integer
    Dim Combination() As Integer
    Dim NumCandidates As Integer
    Dim NumCombination As Integer
    Dim i As Integer, j As Integer
    Dim OutputSheet As Worksheet
  
    NumCandidates = 20
    NumCombination = 5
  
    Set OutputSheet = ThisWorkbook.Sheets("Sheet1") '<~~ Change sheet as needed
  
    ReDim Candidates(1 To NumCandidates)
    For i = 1 To NumCandidates
        Candidates(i) = i
    Next i
  
    ReDim Combination(1 To NumCombination)
  
    OutputSheet.Cells.Clear
  
    CombinationsRecursive OutputSheet, Candidates, Combination, 1, 1, NumCandidates, NumCombination
End Sub

Sub CombinationsRecursive(OutputSheet As Worksheet, Candidates() As Integer, Combination() As Integer, CurrentIndex As Integer, StartIndex As Integer, NumCandidates As Integer, NumCombination As Integer)
    Dim i As Integer
    Dim OutputRow As Long
  
    If CurrentIndex > NumCombination Then
        OutputRow = OutputSheet.Cells(OutputSheet.Rows.Count, 1).End(xlUp).Row + 1
        For i = 1 To NumCombination
            OutputSheet.Cells(OutputRow, i).Value = Combination(i)
        Next i
        Exit Sub
    End If
  
    For i = StartIndex To NumCandidates
        Combination(CurrentIndex) = Candidates(i)
        GenerateCombinationsRecursiveToSheet OutputSheet, Candidates, Combination, CurrentIndex + 1, i + 1, NumCandidates, NumCombination
    Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,094
Messages
6,123,069
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top