I need help with a combination generator.
I found some good startpoints here, but the result is too long, I need an extra filter.
In my (borrowed) example there are 4 colums of which 2 pairs are identical. (most of the times)
The combination I am looking for must skip the combination where the value from column A is identical to the value in column C, and also where the value from column B is identical to the value in column D.
In other words: result must be completely unique.
this is what I have:
result =
(the result in F is way longer than in my screenshot)
You can already see that 1st result = 101-201-101-102 but this is invalid because there are double values in the result.
The first correct value from this list is the 10th one: 101-201-102-202
And it would be superb if result is also in 4 columns. e.g.: column F, G, H, I filled with the seperate values.
Of course column A, B, C and D are dynamic, so tomorrow list A and C are maybe 4 rows long and column B and D 12 rows.
and sometimes all 4 columns are the same. so then the first result correct could be 101-102-103-104.
And if I have a working result, then I like to have some tweaking parameters implemented, but above question is the most important, lets start with that.
Thanks in advance!
I found some good startpoints here, but the result is too long, I need an extra filter.
In my (borrowed) example there are 4 colums of which 2 pairs are identical. (most of the times)
The combination I am looking for must skip the combination where the value from column A is identical to the value in column C, and also where the value from column B is identical to the value in column D.
In other words: result must be completely unique.
this is what I have:
VBA Code:
Sub 4-column-combi()
Dim xDRg1, xDRg2, xDRg3, xDRg4 As Range
Dim xRg As Range
Dim xStr As String
Dim xFN1, xFN2, xFN3, xFN4 As Integer
Dim xSV1, xSV2, xSV3, xSV4 As String
Columns("F:F").Select
Selection.ClearContents
Range("F1").Select
Set xDRg1 = Range("A1:A15") 'First column data
Set xDRg2 = Range("B1:B8") 'Second column data
Set xDRg3 = Range("C1:C15") 'Third column data
Set xDRg4 = Range("D1:D8") 'Third column data
xStr = "-" 'Separator
Set xRg = Range("F1") 'Output cell
For xFN1 = 1 To xDRg1.Count
xSV1 = xDRg1.Item(xFN1).Text
For xFN2 = 1 To xDRg2.Count
xSV2 = xDRg2.Item(xFN2).Text
For xFN3 = 1 To xDRg3.Count
xSV3 = xDRg3.Item(xFN3).Text
For xFN4 = 1 To xDRg4.Count
xSV4 = xDRg4.Item(xFN4).Text
xRg.Value = xSV1 & xStr & xSV2 & xStr & xSV3 & xStr & xSV4
Set xRg = xRg.Offset(1, 0)
Next
Next
Next
Next
End Sub
result =
LB1.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | 101 | 201 | 101 | 201 | 101-201-101-201 | |||
2 | 102 | 202 | 102 | 202 | 101-201-101-202 | |||
3 | 103 | 203 | 103 | 203 | 101-201-101-203 | |||
4 | 104 | 204 | 104 | 204 | 101-201-101-204 | |||
5 | 105 | 205 | 105 | 205 | 101-201-101-205 | |||
6 | 106 | 206 | 106 | 206 | 101-201-101-206 | |||
7 | 107 | 207 | 107 | 207 | 101-201-101-207 | |||
8 | 108 | 208 | 108 | 208 | 101-201-101-208 | |||
9 | 109 | 109 | 101-201-102-201 | |||||
10 | 110 | 110 | 101-201-102-202 | |||||
11 | 111 | 111 | 101-201-102-203 | |||||
12 | 112 | 112 | 101-201-102-204 | |||||
13 | 113 | 113 | 101-201-102-205 | |||||
14 | 114 | 114 | 101-201-102-206 | |||||
15 | 115 | 115 | 101-201-102-207 | |||||
16 | 101-201-102-208 | |||||||
17 | 101-201-103-201 | |||||||
18 | 101-201-103-202 | |||||||
Blad1 |
(the result in F is way longer than in my screenshot)
You can already see that 1st result = 101-201-101-102 but this is invalid because there are double values in the result.
The first correct value from this list is the 10th one: 101-201-102-202
And it would be superb if result is also in 4 columns. e.g.: column F, G, H, I filled with the seperate values.
Of course column A, B, C and D are dynamic, so tomorrow list A and C are maybe 4 rows long and column B and D 12 rows.
and sometimes all 4 columns are the same. so then the first result correct could be 101-102-103-104.
And if I have a working result, then I like to have some tweaking parameters implemented, but above question is the most important, lets start with that.
Thanks in advance!