Share via

Find Unique Data Combinations in a Series of Rows

Anonymous
2020-10-21T14:17:25+00:00

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!

Microsoft 365 and Office | Excel | For home | Windows

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.

0 comments No comments

Answer accepted by question author

Anonymous
2020-11-03T18:37:29+00:00

I think it has to do with your browser and how the code is copied and pasted. Here is a working file:

https://1drv.ms/x/s!AsKdy7Nfg\_Fbi2yilqueUeWpf5Tu?e=wzm67W

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

30 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-10-23T14:40:12+00:00

    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

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-10-23T13:21:41+00:00

    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.....

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-10-23T10:26:11+00:00

    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?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-10-22T14:38:31+00:00

    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

    Was this answer helpful?

    0 comments No comments