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
n
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
Book3.xlsb | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | User | 2-item combinations no repeats | Desired Result | ||||||
2 | Input | ||||||||
3 | |||||||||
4 | List | 1 | 2 | Seq. | 1 | 2 | |||
5 | Apple | Apple | Orange | Apple | Orange | ||||
6 | Orange | Apple | Banana | Apple | Banana | ||||
7 | Banana | Apple | 0 | Orange | Banana | ||||
8 | 0 | Apple | 0 | ||||||
9 | 0 | Orange | Banana | ||||||
10 | Orange | 0 | |||||||
11 | Orange | 0 | |||||||
12 | Banana | 0 | |||||||
13 | Banana | 0 | |||||||
14 | 0 | 0 | |||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B5:C15 | B5 | =Combinations(A5:A9,2) |
Dynamic array formulas. |