all possible combination formula for numbers

awanone

New Member
Joined
Jul 26, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hi All,
I would like to find out a formula for the following situation, i have the following 5 integers lets say A, B, C, D, E (they can be numbers in between 0 to 4 & one number can repeat in the sequence like 01202 here 2 and 0 is repeating ) , i would like to see all the possible combinations for example ( as example stated 01202 would want to see all combinations possible such as 00122, 22001,12020 so on ... ). so is there any formula in excel where I can give the numbers and it can provide me with all possible combinations, thanks for the help
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
want to see all combinations

Maybe this idea can help you.
The order of your digits is in vertical order.

On Sheet1 set up this arrangement.

Please see Image
combinations-permutations.png


Copy this VBA code below into the standard Module and run 'ListPermutationsOrCombinations'.
Code:
'Option Explicit
Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet
' Myrna Larson, July 25, 2000, Microsoft.Public.Excel.Misc

Sub ListPermutationsOrCombinations()
Dim Rng As Range
Dim PopSize As Integer
Dim SetSize As Integer
Dim Which As String
Dim n As Double
Const BufferSize As Long = 4096

Worksheets("Sheet1").Range("A1").Select
Set Rng = Selection.Columns(1).Cells
If Rng.Cells.Count = 1 Then
Set Rng = Range(Rng, Rng.End(xlDown))
End If

PopSize = Rng.Cells.Count - 2
If PopSize < 2 Then GoTo DataError

SetSize = Rng.Cells(2).Value
If SetSize > PopSize Then GoTo DataError

Which = UCase$(Rng.Cells(1).Value)
Select Case Which
Case "C"
n = Application.WorksheetFunction.Combin(PopSize, SetSize)
Case "P"
n = Application.WorksheetFunction.Permut(PopSize, SetSize)
Case Else
GoTo DataError
End Select
'If n > Cells.Count Then GoTo DataError 'EDITED OUT BY VoG

Application.ScreenUpdating = False

Set Results = Worksheets.Add

vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
ReDim Buffer(1 To BufferSize) As String
BufferPtr = 0

If Which = "C" Then
AddCombination PopSize, SetSize
Else
AddPermutation PopSize, SetSize
End If
vAllItems = 0

Application.ScreenUpdating = True
Exit Sub

DataError:
If n = 0 Then
Which = "Enter your data in a vertical range of at least 4 cells." _
& String$(2, 10) _
& "Top cell must contain the letter C or P, 2nd cell is the Number" _
& "of items in a subset, the cells below are the values from Which" _
& "the subset is to be chosen."

Else
Which = "This requires " & Format$(n, "#,##0") & _
" cells, more than are available on the worksheet!"
End If
MsgBox Which, vbOKOnly, "DATA ERROR"
Exit Sub
End Sub

Private Sub AddPermutation(Optional PopSize As Integer = 0, _
Optional SetSize As Integer = 0, _
Optional NextMember As Integer = 0)

Static iPopSize As Integer
Static iSetSize As Integer
Static SetMembers() As Integer
Static Used() As Integer
Dim i As Integer

If PopSize <> 0 Then
iPopSize = PopSize
iSetSize = SetSize
ReDim SetMembers(1 To iSetSize) As Integer
ReDim Used(1 To iPopSize) As Integer
NextMember = 1
End If

For i = 1 To iPopSize
If Used(i) = 0 Then
SetMembers(NextMember) = i
If NextMember <> iSetSize Then
Used(i) = True
AddPermutation , , NextMember + 1
Used(i) = False
Else
SavePermutation SetMembers()
End If
End If
Next i

If NextMember = 1 Then
SavePermutation SetMembers(), True
Erase SetMembers
Erase Used
End If

End Sub 'AddPermutation

Private Sub AddCombination(Optional PopSize As Integer = 0, _
Optional SetSize As Integer = 0, _
Optional NextMember As Integer = 0, _
Optional NextItem As Integer = 0)

Static iPopSize As Integer
Static iSetSize As Integer
Static SetMembers() As Integer
Dim i As Integer

If PopSize <> 0 Then
iPopSize = PopSize
iSetSize = SetSize
ReDim SetMembers(1 To iSetSize) As Integer
NextMember = 1
NextItem = 1
End If

For i = NextItem To iPopSize
SetMembers(NextMember) = i
If NextMember <> iSetSize Then
AddCombination , , NextMember + 1, i + 1
Else
SavePermutation SetMembers()
End If
Next i

If NextMember = 1 Then
SavePermutation SetMembers(), True
Erase SetMembers
End If

End Sub 'AddCombination

Private Sub SavePermutation(ItemsChosen() As Integer, _
Optional FlushBuffer As Boolean = False)

Dim i As Integer, sValue As String
Static RowNum As Long, ColNum As Long

If RowNum = 0 Then RowNum = 1
If ColNum = 0 Then ColNum = 1

If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
If BufferPtr > 0 Then
If (RowNum + BufferPtr - 1) > Rows.Count Then
RowNum = 1
ColNum = ColNum + 1
If ColNum > 256 Then Exit Sub
End If

Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
= Application.WorksheetFunction.Transpose(Buffer())
RowNum = RowNum + BufferPtr
End If

BufferPtr = 0
If FlushBuffer = True Then
Erase Buffer
RowNum = 0
ColNum = 0
Exit Sub
Else
ReDim Buffer(1 To UBound(Buffer))
End If
End If

'construct the next set
For i = 1 To UBound(ItemsChosen)
sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1) 'set delimiter ", " or " & "
Next i

'and save it in the buffer
BufferPtr = BufferPtr + 1
Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub 'SavePermutation

This VBA macro will create a list of all possible permutations (combinations) for 5 numbers (on Sheet2).
 
Upvote 0
another approach with Power Query
#1#2#3#4#5Number
0123400000
00001
00002
00003
00004
00010
00011
00012
00013
00014
00020
00021
00022
00023
00024
00030
00031
00032
00033
00034
00040
and so on…

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transpose = Table.Transpose(Source),
    ExpandFirst = Table.ExpandListColumn(Table.AddColumn(Transpose, "Custom", each {0..4}), "Custom"),
    ExpandSecond = Table.ExpandListColumn(Table.AddColumn(ExpandFirst, "Custom.1", each {0..4}), "Custom.1"),
    ExpandThird = Table.ExpandListColumn(Table.AddColumn(ExpandSecond, "Custom.2", each {0..4}), "Custom.2"),
    ExpandFourth = Table.ExpandListColumn(Table.AddColumn(ExpandThird, "Custom.3", each {0..4}), "Custom.3"),
    Type = Table.TransformColumnTypes(ExpandFourth,{{"Column1", type text}, {"Custom", type text}, {"Custom.1", type text}, {"Custom.2", type text}, {"Custom.3", type text}}),
    Join = Table.AddColumn(Type, "Number", each [Column1]&[Custom]&[Custom.1]&[Custom.2]&[Custom.3]),
    SortAsc = Table.Sort(Table.Distinct(Table.SelectColumns(Join,{"Number"})),{{"Number", Order.Ascending}})
in
    SortAsc
 
Last edited:
Upvote 0
@sandy666

Here your numbers are Repeating. Say 00001. 0 cannot repeat more than once. It is a case of combination. But your answer is based on permutation. I hope you can provide us some answer using power query.
 
Upvote 0
@sandy666, I would (almost) agree with @CA_Punit

The way that I read the OP, 0 (or any digit) can not repeat more (or less) times than it appears in the starting string of 5 digits.

With the example starting digits of 01202, the result should be any combination that uses 0 twice, 2 twice and 1 once. Combinations that have 0 or 2 more or less than twice, or 1 more than once should be excluded. This would always give 120 results, although that would include technical duplicates, e.g. 01202 and 01202
 
Upvote 0
sure but I still prefer post#3 until OP define word "repeat"
anyway here is on cito
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Transpose = Table.Transpose(Source),
    ExpandFirst = Table.ExpandListColumn(Table.AddColumn(Transpose, "Custom", each {0..4}), "Custom"),
    ExpandSecond = Table.ExpandListColumn(Table.AddColumn(ExpandFirst, "Custom.1", each {0..4}), "Custom.1"),
    ExpandThird = Table.ExpandListColumn(Table.AddColumn(ExpandSecond, "Custom.2", each {0..4}), "Custom.2"),
    ExpandFourth = Table.ExpandListColumn(Table.AddColumn(ExpandThird, "Custom.3", each {0..4}), "Custom.3"),
    Type = Table.TransformColumnTypes(ExpandFourth,{{"Column1", type text}, {"Custom", type text}, {"Custom.1", type text}, {"Custom.2", type text}, {"Custom.3", type text}}),
    Join = Table.AddColumn(Type, "Number", each [Column1]&[Custom]&[Custom.1]&[Custom.2]&[Custom.3]),
    SortAsc = Table.Sort(Table.Distinct(Table.SelectColumns(Join,{"Number"})),{{"Number", Order.Ascending}}),
    Filter0 = Table.SelectRows(Table.AddColumn(SortAsc, "Custom", each List.Count(Text.Split([Number],"0"))-1), each ([Custom] = 0 or [Custom] = 1 or [Custom] = 2)),
    Filter1 = Table.SelectRows(Table.AddColumn(Filter0, "Custom.1", each List.Count(Text.Split([Number],"1"))-1), each ([Custom.1] = 0 or [Custom.1] = 1)),
    Filter2 = Table.SelectRows(Table.AddColumn(Filter1, "Custom.2", each List.Count(Text.Split([Number],"2"))-1), each ([Custom.2] = 0 or [Custom.2] = 1 or [Custom.2] = 2)),
    Filter3 = Table.SelectRows(Table.AddColumn(Filter2, "Custom.3", each List.Count(Text.Split([Number],"3"))-1), each ([Custom.3] = 0 or [Custom.3] = 1)),
    Filter4 = Table.SelectRows(Table.AddColumn(Filter3, "Custom.4", each List.Count(Text.Split([Number],"4"))-1), each ([Custom.4] = 0 or [Custom.4] = 1)),
    TSC = Table.SelectColumns(Filter4,{"Number"})
in
    TSC
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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