A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
I think it has to do with your browser and how the code is copied and pasted. Here is a working file:
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm trying to find unique data combinations in a series of rows. I have a range of numbers, with random numbers being assigned to each row.
I'm trying to find all of the unique combinations of this data that does not appear in the same row.
I.e. all of the unique combinations of two pieces of data.
All of the unique combinations of three pieces of data (not including the two piece combinations).
All of the unique combinations of four pieces of data (not including the two or three piece combinations).
All of the unique combinations of five pieces of data (not including the two, three or four piece combinations).
A sample of the data is shown below:
| 1 | 3.5 | 5.5 | 6 | 8 | 10.5 | 12.5 |
|---|---|---|---|---|---|---|
| 1.5 | 3.5 | 4 | 6 | 8.5 | 10.5 | 11 |
| 1.5 | 2 | 4 | 6.5 | 8.5 | 9 | 11.5 |
| 1 | 3 | 5.5 | 7.5 | 8 | 10.5 | 12.5 |
| 1 | 3.5 | 5.5 | 6 | 8.5 | 10.5 | 11 |
| 1 | 3 | 4 | 6 | 8 | 9 | 11 |
| 1.5 | 2 | 4.5 | 6.5 | 7 | 9 | 11.5 |
| 2.5 | 3 | 5.5 | 7.5 | 8 | 10 | 12 |
| 2.5 | 4.5 | 5 | 7.5 | 9.5 | 10 | 12 |
| 2 | 4.5 | 6.5 | 7 | 9.5 | 11.5 | 12 |
| 2.5 | 3 | 5 | 7.5 | 9.5 | 10 | 12.5 |
| 2.5 | 4.5 | 5 | 7 | 9.5 | 11.5 | 12 |
| Value 1 | Value 2 | |||||
| Value 1 | Value 2 | Value 3 | ||||
| Value 1 | Value 2 | Value 3 | Value 4 | |||
| Value 1 | Value 2 | Value 3 | Value 4 | Value 5 |
In the sample, numbers 1 and 1.5 are never in the same row. This would be a two piece unique combination.
In the sample, numbers 1 and 3.5 are in the first row. 3.5 and 4 are in the second row. 1 and 4 are in the sixth row. However, 1, 3.5 and 4 are never in the same row, making this a three value unique combination.
I'm thrilled if someone can display all these unique combinations in some way. It would be ideal if I could select a value and have the other values options change to only show possible unique values, or colour code the options to show which are likely to create a unique combination. e.g. if I need a unique three value combination, let me select the first value and then for value 2 highlight any value that will make it unique in one colour and highlight values that have the potential to make it unique in another. In the third value again highlight the values that would make it unique with the first value in one colour and the value that will be unique as a three value in another.
Many thanks!
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Answer accepted by question author
I think it has to do with your browser and how the code is copied and pasted. Here is a working file:
Thanks Bernie! Take as much time as you need. I really appreciate your help with this!
I have no programming experience.
I've removed all of the extra carriage returns. I now get a Compile error: User-defined type not defined. This is in the first function (after the first Sub)
Function Unique(values As Variant) As Variant()
Dim dict As New Scripting.Dictionary
When you copy code from a website that doesn't use code windows, there are often extra carriage returns placed within the text. So put your cursor at the end of any red line (after the _ underscore continuation character) and press delete to remove the next line that is wholly blank.
I'm guessing that you have no programming experience, so I don't think that you will be able to extend it. Give me some time.....
Thanks Bernie!
I get a compile error: Sub or Function not defined?
Highlighted in yellow:
Sub RemoveNonUniques()
Text in red (under the first End Funciton):
Function ArrayBubbleSort( _
myArray As Variant, _
Optional Ascending As Boolean) _
As Variant
How do I extend the logic to the others?
With your table in starting in A1, the code below will give the pairs. You can extend the logic to do the others.
Option Explicit
Sub RemoveNonUniques()
Dim rngT As Range
Dim rngC As Range
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim strA As String
Dim lngR As Long
Dim vDupes() As Variant
Dim vUniq() As Variant
Dim cGood As Collection
Set rngT = Range("A1").CurrentRegion
vDupes = rngT.Value
vUniq = ArrayBubbleSort(Unique(vDupes), True)
lngR = Cells(Rows.Count, "A").End(xlUp).Row + 5
For i = LBound(vUniq) To UBound(vUniq) - 1
Set cGood = New Collection
For j = i + 1 To UBound(vUniq)
cGood.Add vUniq(j)
Next j
strA = ""
Set rngC = rngT.Find(vUniq(i), lookat:=xlWhole)
While rngC.Address <> strA
If strA = "" Then strA = rngC.Address
k = cGood.Count
For j = 1 To k
If j > k Then Exit For
If Not IsError(Application.Match(cGood(j), Intersect(rngT, rngC.EntireRow), False)) Then
cGood.Remove j
k = k - 1
j = j - 1
End If
Next j
Set rngC = rngT.FindNext(rngC)
Wend
For j = 1 To cGood.Count
Cells(lngR, 1).Value = vUniq(i)
Cells(lngR, 2).Value = cGood(j)
lngR = lngR + 1
Next j
Next i
End Sub
Function Unique(values As Variant) As Variant()
Dim dict As New Scripting.Dictionary
Dim val As Variant
For Each val In values
dict(val) = 1
Next
Unique = dict.Keys
End Function
Function ArrayBubbleSort( _
myArray As Variant, _
Optional Ascending As Boolean) _
As Variant
Dim myTemp As Variant
Dim myInt() As Variant
Dim i As Integer
Dim j As Integer
ReDim myInt(LBound(myArray) To UBound(myArray))
For i = LBound(myArray) To UBound(myArray)
myInt(i) = val(Trim(myArray(i)))
Next i
'Do the sort
For i = LBound(myInt) To UBound(myInt) - 1
For j = i + 1 To UBound(myInt)
If Ascending Then
If myInt(i) > myInt(j) Then
myTemp = myInt(j)
myInt(j) = myInt(i)
myInt(i) = myTemp
End If
Else
If myInt(i) < myInt(j) Then
myTemp = myInt(j)
myInt(j) = myInt(i)
myInt(i) = myTemp
End If
End If
Next j
Next i
'Return the array
ArrayBubbleSort = myInt
End Function