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.
 
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))
This code is working with more rows and I can make it work on my end. Thank you for taking the time.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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