Unique Combination of Values

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hi, Peter

I found #post3 code very useful for me and I am using it. I want if you could adapt my need will be great of you.

I want to find unique among any of 14 columns B To O, selecting continuous or alternate 1, 2, 4, 6, or all 14
In example below I have shown 3 columns C, G, K in my selection.


Book1
ABCDEFGHIJKLMNO
1DateP1P2P3P4P5P6P7P8P9P10P11P12P13P14
2DateP1P2P3P4P5P6P7P8P9P10P11P12P13P14
301/12/20132X2111X1112X11
402/12/201322X121XX21212X
503/12/201311111X1X11XX12
604/12/201311121112X111X1
705/12/20131X11X12X11111X
806/12/201311122X111X111X
907/12/201311X111121X11X1
1008/12/2013121111211X1112
1109/12/201322X2112X211X12
1210/12/2013X1111221121X11
1311/12/20131211X1111XX1XX
1412/12/2013112X2212X11X21
1513/12/20131X1X1221111222
1614/12/2013XX11211222X111
1715/12/201322222X1112X2X1
1816/12/2013X111XXX111X122
1917/12/201322X1212121X11X
2018/12/2013X1121121X2X221
2119/12/2013111212X22X1211
2220/12/20132111X211X1X122
Sheet1


I want result in sheet 2 as shown column B count match.


Book1
ABCDE
1P2|P6|P10Count MatchP2P6P10
2X|1|12X11
32|1|13211
41|X|121X1
51|1|11111
61|X|X11XX
71|1|X111X
82|1|X221X
91|2|21122
101|2|12121
11X|2|11X21
12X|1|21X12
132|X|212X2
141|1|21112
151|2|X112X
Sheet2


Using Excel Version 2000

Thank You

Regards,
Kishan

<colgroup><col><col><col span="3"></colgroup><tbody>
</tbody>
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Best to start your own thread and provide a link to the related one, rather than 'hijacking' somebody else's so I've moved your post and added the link.
In due course I will return here and see if I am able to help. Not sure at this stage.
 
Upvote 0
Try this in a copy of your workbook.

I have assumed ..
- data starts in A1 of Sheet1 and is bounded by an empty column on the right, an empty row below & no gaps in column A dates.
- Sheet1 is active when the code is run
- you don't need to select whole columns, any cells will do. For example, selecting C5:D6, H15 and M1 will process the data for columns C, D, H and M as if those whole columns were selected.

Note, though, that my code processes the columns in the order you select them. So for your selection above, if you selected column C, then column K, then column G the results would appear in that order in Sheet2. That may be an advantage or a problem for you. If it turns out to be a problem, post back & an alternative would be offered.

Anyway, give it a go.

Rich (BB code):
Sub UniqueListAndCount()
  Dim a, vRws, vCols
  Dim d As Object
  Dim i As Long
  Dim c  As Range
  Dim s As String
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  vRws = Evaluate("Row(2:" & Range("A1").End(xlDown).Row & ")")
  ReDim vCols(0 To 0)
  For Each c In Intersect(Selection.EntireColumn, Rows(1))
    i = i + 1
    ReDim Preserve vCols(1 To i)
    vCols(i) = c.Column
  Next c
  a = Application.Index(Range("A1").CurrentRegion, vRws, vCols)
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, 0), "|")
    If d.exists(s) Then
      d.Item(s) = d.Item(s) + 1
    Else
      d.Add s, 1
    End If
  Next i
  With Sheets("Sheet2")
    .UsedRange.ClearContents
    With .Range("A1").Resize(d.Count, 2)
      .Value = Application.Transpose(Array(d.keys, d.items))
      .Columns(1).TextToColumns Destination:=.Cells(1, 3), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="|"
      .Cells(1, 2).Value = "Count Match"
    End With
  End With
End Sub


Excel Workbook
ABCDEFGHIJKLMNOP
1DateP1P2P3P4P5P6P7P8P9P10P11P12P13P14
2DateP1P2P3P4P5P6P7P8P9P10P11P12P13P14
31/12/20132X2111X1112X11
42/12/201322X121XX21212X
53/12/201311111X1X11XX12
64/12/201311121112X111X1
75/12/20131X11X12X11111X
86/12/201311122X111X111X
97/12/201311X111121X11X1
108/12/2013121111211X1112
119/12/201322X2112X211X12
1210/12/2013X1111221121X11
1311/12/20131211X1111XX1XX
1412/12/2013112X2212X11X21
1513/12/20131X1X1221111222
1614/12/2013XX11211222X111
1715/12/201322222X1112X2X1
1816/12/2013X111XXX111X122
1917/12/201322X1212121X11X
2018/12/2013X1121121X2X221
2119/12/2013111212X22X1211
2220/12/20132111X211X1X122
23
Sheet1



With those green cells selected as described above, these are my results:
 
Upvote 0
I wasn't able to post this image in my last post, so here it is.

Excel Workbook
ABCDEFG
1P2|P3|P7|P12Count MatchP2P3P7P12
2X|2|X|X1X2XX
32|X|X|112XX1
41|1|1|X1111X
51|1|1|131111
6X|1|2|11X121
71|X|1|111X11
82|1|2|112121
92|X|2|X12X2X
101|1|2|X1112X
112|1|1|112111
121|2|1|X1121X
13X|1|2|21X122
14X|1|1|11X111
152|2|1|212212
161|1|X|1111X1
172|X|2|112X21
181|1|2|211122
191|1|X|2111X2
20
Sheet2
 
Upvote 0
Best to start your own thread and provide a link to the related one
Hi Peter,
Sorry for my ignorance. Thank you... next time I will follow your instructions

Try this in a copy of your workbook.

I have assumed ..
- data starts in A1 of Sheet1 and is bounded by an empty column on the right, an empty row below & no gaps in column A dates.
- Sheet1 is active when the code is run
- you don't need to select whole columns, any cells will do. For example, selecting C5:D6, H15 and M1 will process the data for columns C, D, H and M as if those whole columns were selected.

Note, though, that my code processes the columns in the order you select them. So for your selection above, if you selected column C, then column K, then column G the results would appear in that order in Sheet2. That may be an advantage or a problem for you. If it turns out to be a problem, post back & an alternative would be offered.

Anyway, give it a go.
Thank you Peter, for giving a key solution it is working superbly!!:)

Selecting process of columns I find very clever, also it is fine that code processes the columns in the order as select them.

Thank you once again for your kind help

Regards,
Kishan
 
Upvote 0
Thank you Peter, for giving a key solution it is working superbly!!:)

Selecting process of columns I find very clever, also it is fine that code processes the columns in the order as select them.

Thank you once again for your kind help

Regards,
Kishan
You are welcome. Glad you liked it. :)
 
Upvote 0
You are welcome. Glad you liked it. :)
I had trouble running your code with a test layout of 56000 rows (the OP's original 20 rows of data repeated to fill the 56000 rows)... it raised a "Subscript out of range" error on this line of code...

vCols(i) = c.Column

I am not sure why as that line looks correct... could it have something to do with the number of rows of data being processed? The reason I tried to run your code was I wanted to time test it against the code I developed below. With all 15 columns selected (I figured that would produce the longest run timesJ), it processes the 56000 rows of data in 0.88 seconds. I was wondering if the Dictionary approach would be faster or not. Anyway, here is the code I came up with...
Code:
[table="width: 500"]
[tr]
	[td]Sub UniqueListAndCountToo()
  Dim X As Long, LastRow As Long, Addr As String, Data As Variant, CountMatch As Variant
  LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To 15
    If Not Intersect(Selection.EntireColumn, Cells(1, X)) Is Nothing Then Addr = Addr & "&""|""&" & Cells(1, X).Resize(LastRow).Address(0, 0)
  Next
  Addr = Mid(Addr, 6)
  With Sheets("Sheet2")
    .UsedRange.Clear
    Data = Evaluate(Addr)
    .Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row) = Data
    .Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
    .Range("A1", .Cells(Rows.Count, "A").End(xlUp)).TextToColumns Range("C1"), xlDelimited, , , 0, 0, 0, 0, True, "|"
    .Range("B1").Value = "Count Match"
    Data = WorksheetFunction.Transpose(Data)
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    ReDim CountMatch(1 To LastRow - 1, 1 To 1)
    For X = 2 To LastRow
      CountMatch(X - 1, 1) = 1 + UBound(Filter(Data, .Cells(X, "A").Value))
    Next
    .Range("B2").Resize(UBound(CountMatch)) = CountMatch
    With .Columns("A").Resize(, .Cells(1, Columns.Count).End(xlToLeft).Column)
      .AutoFit
      .HorizontalAlignment = xlCenter
    End With
  End With
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I had trouble running your code with a test layout of 56000 rows (the OP's original 20 rows of data repeated to fill the 56000 rows)... it raised a "Subscript out of range" error on this line of code...

vCols(i) = c.Column

I am not sure why as that line looks correct... could it have something to do with the number of rows of data being processed? The reason I tried to run your code was I wanted to time test it against the code I developed below. With all 15 columns selected (I figured that would produce the longest run timesJ), it processes the 56000 rows of data in 0.88 seconds. I was wondering if the Dictionary approach would be faster or not. Anyway, here is the code I came up with...
Code:
[table="width: 500"]
[tr]
	[td]Sub UniqueListAndCountToo()
  Dim X As Long, LastRow As Long, Addr As String, Data As Variant, CountMatch As Variant
  LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To 15
    If Not Intersect(Selection.EntireColumn, Cells(1, X)) Is Nothing Then Addr = Addr & "&""|""&" & Cells(1, X).Resize(LastRow).Address(0, 0)
  Next
  Addr = Mid(Addr, 6)
  With Sheets("Sheet2")
    .UsedRange.Clear
    Data = Evaluate(Addr)
    .Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row) = Data
    .Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
    .Range("A1", .Cells(Rows.Count, "A").End(xlUp)).TextToColumns Range("C1"), xlDelimited, , , 0, 0, 0, 0, True, "|"
    .Range("B1").Value = "Count Match"
    Data = WorksheetFunction.Transpose(Data)
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    ReDim CountMatch(1 To LastRow - 1, 1 To 1)
    For X = 2 To LastRow
      CountMatch(X - 1, 1) = 1 + UBound(Filter(Data, .Cells(X, "A").Value))
    Next
    .Range("B2").Resize(UBound(CountMatch)) = CountMatch
    With .Columns("A").Resize(, .Cells(1, Columns.Count).End(xlToLeft).Column)
      .AutoFit
      .HorizontalAlignment = xlCenter
    End With
  End With
End Sub[/td]
[/tr]
[/table]
I forgot to mention... the above code always processes the columns from left to right no matter what order you select the cells that designate those columns in.
 
Upvote 0
... could it have something to do with the number of rows of data being processed?
It could, but until advised otherwise, I'm assuming data size similar to that posted.

I was wondering if the Dictionary approach would be faster or not.
I suspect the dictionary would be slower if very large data.

Anyway, here is the code I came up with...
Two remarks:

1. I would be very cautious about using Remove Duplicates. Unless M/Soft have fixed it in the latest version(s), it is unreliable & can fail without producing an error message. A repeatable example (for me anyway)

2. You would need to adjust all your counts up 1, remembering that Filter() produced a zero-based array.
 
Upvote 0
Anyway, here is the code I came up with...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub UniqueListAndCountToo()
  Dim X As Long, LastRow As Long, Addr As String, Data As Variant, CountMatch As Variant
  LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To 15
    If Not Intersect(Selection.EntireColumn, Cells(1, X)) Is Nothing Then Addr = Addr & "&""|""&" & Cells(1, X).Resize(LastRow).Address(0, 0)
  Next
  Addr = Mid(Addr, 6)
  With Sheets("Sheet2")
    .UsedRange.Clear
    Data = Evaluate(Addr)
    .Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row) = Data
    .Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
    .Range("A1", .Cells(Rows.Count, "A").End(xlUp)).TextToColumns Range("C1"), xlDelimited, , , 0, 0, 0, 0, True, "|"
    .Range("B1").Value = "Count Match"
    Data = WorksheetFunction.Transpose(Data)
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    ReDim CountMatch(1 To LastRow - 1, 1 To 1)
    For X = 2 To LastRow
      CountMatch(X - 1, 1) = 1 + UBound(Filter(Data, .Cells(X, "A").Value))
    Next
    .Range("B2").Resize(UBound(CountMatch)) = CountMatch
    With .Columns("A").Resize(, .Cells(1, Columns.Count).End(xlToLeft).Column)
      .AutoFit
      .HorizontalAlignment = xlCenter
    End With
  End With
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Hi Rick Rothstein,

I am trying code with my data #post1 selecting column C, G, K it produce error ‘438’ after debug highlight following line.
Code:
.Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
.
after re-establish if I go to sheet2 I get result as shown below


Book1
A
1P2|P6|P10
2P2|P6|P10
3X|1|1
42|1|1
51|X|1
61|1|1
7X|1|1
81|X|X
91|1|X
102|1|X
112|1|1
121|2|2
132|1|X
141|2|1
15X|2|1
16X|1|2
172|X|2
181|X|1
192|1|1
201|1|2
211|2|X
221|2|1
Sheet2


Please could you check it?

Thank you

Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,426
Members
448,961
Latest member
nzskater

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