combinations generater?

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
510
Office Version
  1. 365
Platform
  1. Windows
hi!
i have 4 numbers in 4 cells:
a1=1 b1=2 c1=3 d1=4
(could be also 1-2-3-3)
how to generate all possible combinations of 4 numbers
out of 8 (1-2-3-4-5-6-7-8)
with repeats
and two out of the 4's (can be3's too)
need to be from the original range (a1:d1),
like 2-1-7-7 or 4,4,8,6 ?

very favorable by formula/s if possible

p.s
order not important (like 2-1-3-4 and also 3-2-1-4)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Sounds like your version of excel doesn't have one or more of the functions I used. So power query or vba may be your best rout.

for match list of 1-2-3-4 is the following correct?

1-1-1-1 no good
1-2-1-2 keep
1-1-1-3 keep
8-1-1-3 keep
1-1-1-8 no good

And the 1-2-3-4 list is not static, meaning you might change it w-x-y-z where w,x,y,z are integers in [1,8]?
 
Upvote 0
Without lambda and byrows functions I don't know how to spill the results from a single cell formula. But here's an approach using helper columns:

columns F:I spill from a single formula

column J needs to be copied down (to J4096)

Result is in columns L:O spilling from a single formula

First few rows depicted below

Book3
ABCDEFGHIJKLMNOP
112341111FALSE1112
21112TRUE1113
31113TRUE1114
41114TRUE1121
51115FALSE1122
61116FALSE1123
71117FALSE1124
81118FALSE1125
91121TRUE1126
101122TRUE1127
111123TRUE1128
121124TRUE1131
131125TRUE1132
141126TRUE1133
151127TRUE1134
161128TRUE1135
171131TRUE1136
181132TRUE1137
191133TRUE1138
201134TRUE1141
211135TRUE1142
Sheet1 (2)
Cell Formulas
RangeFormula
A1:D1A1=SEQUENCE(1,4)
F1:I4096F1= MOD(INT((SEQUENCE(8^4)-0.5)/8^SEQUENCE(1,4,3,-1)),8)+1
L1:O2364L1=FILTER(F1#,J1:J4096)
J1:J21J1=SUM(--(COUNTIF(F1:I1,A$1#)>0))>1
Dynamic array formulas.


if you want to use power query here is revised PQ M code:
Power Query:
let
    lst0 = Record.ToList(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]{0}),
    lst = {1..8},
    tbl = Table.FromColumns({{lst}},{"Column1"}),
    tbl1 = List.Accumulate({2..4}, tbl, (s,c)=> Table.AddColumn(s, "Column" & Text.From(c), each lst)),
    tbl2 = List.Accumulate({1..4}, tbl1, (s,c)=> Table.ExpandListColumn(s, "Column" & Text.From(c))),
    Result = Table.SelectRows(tbl2, each  List.Accumulate(lst0, {Record.ToList(_),0}, (s,c) => let p = List.PositionOf(s{0},c) in if p>=0 then {List.RemoveRange(s{0},p,1), s{1}+1} else s){1} >1)
in
    Result
 
Upvote 0
How about
Fluff.xlsm
ABCD
11235
2
31112
41113
51115
61121
71122
81123
91124
101125
111126
121127
131128
141131
151132
161133
171134
181135
191136
201137
211138
221142
231143
241145
251151
261152
271153
281154
291155
301156
311157
321158
331162
341163
351165
361172
371173
381175
391182
401183
Data
Cell Formulas
RangeFormula
A3:D2366A3=LET(b,BASE(SEQUENCE(8^4,,0),8,4)+"1111",m,--MID(b,{1,2,3,4},1),FILTER(m,(ISNUMBER(FIND(A1,b)))+(ISNUMBER(FIND(B1,b)))+(ISNUMBER(FIND(C1,b)))+(ISNUMBER(FIND(D1,b)))>1))
Dynamic array formulas.
 
Upvote 0
How about
Fluff.xlsm
ABCD
11235
2
31112
41113
51115
61121
71122
81123
91124
101125
111126
121127
131128
141131
151132
161133
171134
181135
191136
201137
211138
221142
231143
241145
251151
261152
271153
281154
291155
301156
311157
321158
331162
341163
351165
361172
371173
381175
391182
401183
Data
Cell Formulas
RangeFormula
A3:D2366A3=LET(b,BASE(SEQUENCE(8^4,,0),8,4)+"1111",m,--MID(b,{1,2,3,4},1),FILTER(m,(ISNUMBER(FIND(A1,b)))+(ISNUMBER(FIND(B1,b)))+(ISNUMBER(FIND(C1,b)))+(ISNUMBER(FIND(D1,b)))>1))
Dynamic array formulas.
Like the base 8 approach to generating full combinations list!

The filter part works if the a1:d1 are distinct, but if any are repeated it will include rows it shouldn't.

For example if 1-1-2-3 are the match criteria then 1-4-5-6 is included (among others) where it only has one of the matching elements not at least two.
 
Upvote 0
If you're interested in a macro, how about
VBA Code:
Sub excelNewbie()
   Dim Ary As Variant, Nary As Variant
   Dim c As Long, r As Long, nr As Long, x As Long
   Dim Tmp As String
   
   Ary = [Base(Sequence(8 ^ 4, , 0), 8, 4) + "1111"]
   ReDim Nary(1 To UBound(Ary), 1 To 4)
   For r = 1 To UBound(Ary)
      Tmp = Ary(r, 1)
      For c = 1 To 4
         x = InStr(1, Tmp, Cells(1, c))
         If x > 0 Then Tmp = Application.Replace(Tmp, x, 1, "")
      Next c
      If Len(Tmp) < 3 Then
         nr = nr + 1
         For c = 1 To 4
            Nary(nr, c) = Mid(Ary(r, 1), c, 1) + 0
         Next c
      End If
   Next r
   Range("A3").Resize(nr, 4).Value = Nary
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,480
Messages
6,130,905
Members
449,606
Latest member
jaybar0812

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