Combinations without zeros - 2 columns

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,

I have been using a neat bit of code I found on Mr. Excel to take two columns of information and return an array of the possible, non-recursive combinations.
The code is below.

Below that is a mini-sheet to help what I am trying to get to.

I would like to have only the array with combinations that don't have a zero in either list like the mini-sheet illustrates.
Unfortunately because the original list comes from other users workbooks and get dynamically fed into a master list which sometimes contains zeros or blanks, I can't easily modify the original list represented in A column example.

I can't seem to figure out how to modify the original VBA to do this, but maybe you will know.
Alternatively, I have tried SEQUENCE, FILTER and some other options, but nothing that quite hits the mark.

Thank you

VBA Code:
Public result() As Variant
 
Function Combinations(rng As Range, n As Single)
Dim b As Single
 
rng1 = rng.Value
b = WorksheetFunction.Combin(UBound(rng1, 1), n)
 
ReDim result(b, n - 1)
Call Recursive(rng1, n, 1, 0, 0)
 
For g = 0 To UBound(result, 2)
     result(UBound(result, 1), g) = ""
Next g
 
Combinations = result
 
End Function


Function Recursive(r As Variant, c As Single, d As Single, e As Single, h As Single)
Dim f As Single
 
For f = d To UBound(r, 1)
     
        result(h, e) = r(f, 1)
     
        If e = (c - 1) Then
                 
            For g = 0 To UBound(result, 2)
                result(h + 1, g) = result(h, g)
            Next g
            h = h + 1
        Else
            Call Recursive(r, c, f + 1, e + 1, h)
        End If
     
Next f
     
End Functio
n


Book3.xlsb
ABCDEFG
1User2-item combinations no repeatsDesired Result
2Input
3
4List12Seq.12
5AppleAppleOrangeAppleOrange
6OrangeAppleBananaAppleBanana
7BananaApple0OrangeBanana
80Apple0
90OrangeBanana
10Orange0
11Orange0
12Banana0
13Banana0
1400
Sheet1
Cell Formulas
RangeFormula
B5:C15B5=Combinations(A5:A9,2)
Dynamic array formulas.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about
Fluff.xlsm
ABC
1User2-item combinations no repeats
2Input
3
4List12
5AppleAppleOrange
6OrangeAppleBanana
7BananaOrangeBanana
80
90
Data
Cell Formulas
RangeFormula
B5:C7B5=LET(Rng,FILTER(A5:A9,A5:A9<>0),r,ROWS(Rng),b,BASE(SEQUENCE(r^2,,0),r,2),f,FILTER(b,LEFT(b)<>RIGHT(b)),m,MID(f,{1,2},1)+1,by,BYROW(m,LAMBDA(br,CONCAT(SORT(br,,,1)))),rr,ROWS(by),FILTER(INDEX(Rng,m),MMULT((SEQUENCE(rr)>=SEQUENCE(,rr))*(by=TRANSPOSE(by)),SEQUENCE(rr,,,0))=1))
Dynamic array formulas.
 
Upvote 0
Solution
How about
Fluff.xlsm
ABC
1User2-item combinations no repeats
2Input
3
4List12
5AppleAppleOrange
6OrangeAppleBanana
7BananaOrangeBanana
80
90
Data
Cell Formulas
RangeFormula
B5:C7B5=LET(Rng,FILTER(A5:A9,A5:A9<>0),r,ROWS(Rng),b,BASE(SEQUENCE(r^2,,0),r,2),f,FILTER(b,LEFT(b)<>RIGHT(b)),m,MID(f,{1,2},1)+1,by,BYROW(m,LAMBDA(br,CONCAT(SORT(br,,,1)))),rr,ROWS(by),FILTER(INDEX(Rng,m),MMULT((SEQUENCE(rr)>=SEQUENCE(,rr))*(by=TRANSPOSE(by)),SEQUENCE(rr,,,0))=1))
Dynamic array formulas.
Wow Fluff, this works perfectly. I suspect I may never understand the totality of the code itself as LET and LAMBDA are new to me. Thank you for this help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Wow Fluff, this works perfectly. I suspect I may never understand the totality of the code itself as LET and LAMBDA are new to me. Thank you for this help!

You're welcome & thanks for the feedbac
Hi Fluff, wondering if I can ask you another related question. I notice when I try to increase the Rng size past a point then the whole return array comes back as #Value. I was wondering if there was a limitation I should be considering.
 
Upvote 0
How many rows (without 0s) are you talking about?
 
Upvote 0
Around 35-55 or so.
I think I have a solution...if I only increase the range one row at a time and push enter then it works so I can just build it up that way. Not sure why it doesn't do more in one go, but at least it works. Thanks again for your help.
 
Upvote 0
That is a problem, the base function can only handle up to 36, although that will need a tweek to the code for anything above 10
 
Upvote 0
This will handle upto 36 rows of values
Excel Formula:
=LET(Rng,FILTER(A5:A50,A5:A50<>0),r,ROWS(Rng),b,BASE(SEQUENCE(r^2,,0),r,2),f,FILTER(b,LEFT(b)<>RIGHT(b)),m,MID(f,{1,2},1),mm,IF(CODE(m)>64,CODE(m)-54,m+1),by,BYROW(m,LAMBDA(br,CONCAT(SORT(br,,,1)))),rr,ROWS(by),FILTER(INDEX(Rng,mm),MMULT((SEQUENCE(rr)>=SEQUENCE(,rr))*(by=TRANSPOSE(by)),SEQUENCE(rr,,,0))=1))
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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