Count distinct value pairs in multiple columns

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,
Pairs S5 ToRight
Multiple columns combinations R6 ToDown

Data Columns C:P (P1 To P14)
Cell R6 = 1|2|3|4|5|6|7 mean (P1+P2+P3+P4+P5+P6+P7) count Pairs S5 (1|1|1|1|1|1|1) result S6 = 34
Cell R7 = 1|2|3|4|5|6|8 mean (P1+P2+P3+P4+P5+P6+P8) count Pairs S5 (1|1|1|1|1|1|1) result S7 = 28

Example: I have count using AutoFilter as there are 14 columns so COMBIN(14,7) = 3432 I believe need VBA


Book1
ABCDEFGHIJKLMNOPQRSTU
1JorTempP1P2P3P4P5P6P7P8P9P10P11P12P13P14EM 1
2JorTempP1P2P3P4P5P6P7P8P9P10P11P12P13P14EM 1
3JorTempP1P2P3P4P5P6P7P8P9P10P11P12P13P14EM 1
4JorTempP1P2P3P4P5P6P7P8P9P10P11P12P13P14EM 1MultipleCountsCountsCounts
5JorTemp1234567891011121314EM 1Columns1|1|1|1|1|1|1X|X|2|2|1|2|11|1|1|1|1|1|X
6170/7111111111112XX11|2|3|4|5|6|734110
7270/7111111111112XX11|2|3|4|5|6|8287
8370/71111111212111X11|2|3|4|5|6|92515
9470/711111111112X1XX1|2|3|4|5|6|10289
10570/711111111111X2111|2|3|4|5|6|111922
11670/71111111211X12111|2|3|4|5|6|122214
12770/711111111XX212X11|2|3|4|5|6|131215
13870/71111111X11121121|2|3|4|5|6|142613
14970/71111111X1XX222X1|2|3|4|5|7|8
151070/71111111X12111211|2|3|4|5|7|9
161170/711111111X111X121|2|3|4|5|7|10
171270/7111111112X121221|2|3|4|5|7|11
181370/711111111XXX21X21|2|3|4|5|7|12
191470/711111112111X2111|2|3|4|5|7|13
201570/71111111X111X1221|2|3|4|5|7|14
211670/7111111112X21X1X1|2|3|4|5|8|9
221770/711111111111XXXX1|2|3|4|5|8|10
231870/71111111X11XXX111|2|3|4|5|8|11
241970/711111111111XXXX1|2|3|4|5|8|12
252070/7111111111X111211|2|3|4|5|8|13
262170/711111111X1XXX211|2|3|4|5|8|14
272270/7111111111X111211|2|3|4|5|9|10
282370/71111111112111211|2|3|4|5|9|11
292470/711111111112X11X1|2|3|4|5|9|12
302570/7111111111X2122X1|2|3|4|5|9|13
312670/7111111111X1XX221|2|3|4|5|9|14
322770/71111111X11X12211|2|3|4|5|10|11
332870/7111111111X2X2211|2|3|4|5|10|12
342970/7111111111X2122X1|2|3|4|5|10|13
353070/7111111111X1XX221|2|3|4|5|10|14
363170/71111111X11XXX111|2|3|4|5|11|12
373270/711111111111XXXX1|2|3|4|5|11|13
383370/7111111111X111211|2|3|4|5|11|14
393470/7111111112X11X2X1|2|3|4|5|12|13
403570/71111111112111X11|2|3|4|5|12|14
413670/71111111211X22211|2|3|4|5|13|14
423770/71111111112111211|2|3|4|6|7|8
433870/711111111112X11X1|2|3|4|6|7|9
443970/711111111X111X121|2|3|4|6|7|10
454070/711111111X1X11221|2|3|4|6|7|11
46171/721111111112X1XX1|2|3|4|6|7|12
47271/721111111111X2111|2|3|4|6|7|13
48371/72111111112111X11|2|3|4|6|7|14
49471/721111111X21X1X11|2|3|4|6|8|9
50571/72111111X112X1XX1|2|3|4|6|8|10
51671/72111111X121X1X11|2|3|4|6|8|11
52771/7211111111X2X2211|2|3|4|6|8|12
53871/72111111X111X1211|2|3|4|6|8|13
54971/7221X21XXX1XX12X1|2|3|4|6|8|14
551071/72XX21X1121121211|2|3|4|6|9|10
561171/721X2X1212XX112X1|2|3|4|6|9|11
571271/72X2X11111121XXX1|2|3|4|6|9|12
581371/7211X11X11XX11111|2|3|4|6|9|13
591471/7211X111X11111111|2|3|4|6|9|14
601571/7221X111211XXX1X1|2|3|4|6|10|11
611671/72X122X11X1111111|2|3|4|6|10|12
621771/722111221122XX121|2|3|4|6|10|13
631871/72111211111X21111|2|3|4|6|10|14
641971/721X21X1121X221X1|2|3|4|6|11|12
652071/721X1X11XX111X111|2|3|4|6|11|13
662171/72XXX111111111111|2|3|4|6|11|14
672271/72XX22121X11X12X1|2|3|4|6|12|13
682371/7211211111X111111|2|3|4|6|12|14
692471/72X111X1121121XX1|2|3|4|6|13|14
702571/721112X1X11X1XX11|2|3|4|7|8|9
712671/721X1112X1XXX21X1|2|3|4|7|8|10
722771/7211X1XXX111X12X1|2|3|4|7|8|11
732871/721X1X1X22X111121|2|3|4|7|8|12
742971/7211X22111X111121|2|3|4|7|8|13
753071/721XX1X11121111X1|2|3|4|7|8|14
763171/721XXXX1211111211|2|3|4|7|9|10
773271/722X1X111112X1221|2|3|4|7|9|11
783371/7211X122211X1XX21|2|3|4|7|9|12
793471/721111X111111XX11|2|3|4|7|9|13
803571/72111121X11X11111|2|3|4|7|9|14
813671/722X2X121X1X1XX11|2|3|4|7|10|11
823771/7221X111112X111X1|2|3|4|7|10|12
833871/721111X1X12111X21|2|3|4|7|10|13
84172/73X1212XX112X1121|2|3|4|7|10|14
85272/731X2221XX22XX111|2|3|4|7|11|12
86372/731X11XX22XX11XX1|2|3|4|7|11|13
87472/731X1221X11X12211|2|3|4|7|11|14
88572/73X1X1X11X111XX11|2|3|4|7|12|13
89672/73XXX1111X1X212X1|2|3|4|7|12|14
90772/732X1112XXX111211|2|3|4|7|13|14
Sheet1


Thank you in advance

Regards,
Kishan
 
Updated :
Hi Ombir, thank you for your kind help

Your post#7 code does not works as long as data find in column R (1st column pattern in cell (R6) 1|2|3|4|5|6|7 & the last in (cell R3437) 8|9|10|11|12|13|14 total sets = 3432 (it works as long as database) but it works perfect as long as data finds in row S3 To Right.

Only the code works if database is in the range C6:P395 = total 390 lines if 1 more line is added it stop at the following line.

Code:
arr = Application.Index(ar, Evaluate("row(1:" & UBound(ar) & ")"), Sp)

Please could you check?

Kind Regards,
Kishan
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi Rick Rothstein, thank you for your kind help

Your code works perfect as long as data find in column R (1st column pattern in cell (R6) 1|2|3|4|5|6|7 & the last in (cell R3437) 8|9|10|11|12|13|14 total sets = 3432, and also it works perfect as long as data finds in row S3 To Right.

Only the code works if database is in the range C6:P395 = total 390 lines if 1 more line is added it stop at the following line.

Code:
Pattern = Join(Application.Index(Data, X, Split(Results(R, 1), "|")), "|")

I am confused... are you saying your data is not actually where your example showed it to be?
 
Upvote 0
Last edited:
Upvote 0
I am confused... are you saying your data is not actually where your example showed it to be?
Sorry Rick, in reply to your question correct answer is no I have not change my example data layout but when I try with my original database which has 8000+Rows it stop at line shown in the post#10 if I reduce it to 390 lines works perfect. I imagine it is because of version I am using excel 2000

Thank you

Kind Regards,
Kishan
 
Upvote 0
Application.Index has line limiting to use with version 2000 please require modification so the code could work with my database has 8000+ lines

Thank you

Regards,
Kishan
 
Upvote 0
Kishan,

You're from which country ????

Why you're using such an outdated version. 16 years matters a lot.

If you're interested than I can provide you then link to download the latest version.
 
Upvote 0
Kishan,

Why you're using such an outdated version. 16 years matters a lot.

If you're interested than I can provide you then link to download the latest version.
Hi Ombir, I really appreciate that you are thinking about me and offering link to update the software. Which I cannot because then I need to change my older computer also, for the time being I have to wait because my situation does not allows me.

Thank You

Kind Regards,
Kishan
 
Upvote 0
Does this works ??

Code:
Dim x       As Long
Dim r       As Long
Dim temp    As Variant
Dim ele     As Variant
Dim dict    As Object
Sub kishan()
Dim i       As Long
Dim n       As Long
Dim k       As Long
Dim Lr      As Long
Dim rng     As Range
Dim nar     As Variant
Dim Pattern As Variant
Dim out     As Variant
Dim ar      As Variant
Dim arr     As Variant
Dim Sp      As Variant

Application.ScreenUpdating = False

Set dict = CreateObject("scripting.dictionary")

Lr = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("S5").Resize(, Cells(5, Columns.Count).End(xlToLeft).Column - 18)
Pattern = Application.Transpose(Application.Transpose(rng))

ReDim out(1 To Lr, 1 To UBound(Pattern))

ar = Range("C6:P" & Lr)
Set rng = Range("R6:R" & Lr)

For Each cell In rng
        n = n + 1
        Sp = Split(cell, "|")
        arr = Slicearray(ar, Sp)
        ReDim nar(1 To UBound(arr, 1))
        
        For i = 1 To UBound(arr)
            nar(i) = Join(Application.Index(arr, i, 0), "|")
            For k = 1 To UBound(Pattern)
                If Pattern(k) = nar(i) Then
                    out(n, k) = out(n, k) + 1
                    Exit For
                End If
            Next
        Next
Next
Range("S6").Resize(n, UBound(out, 2)) = out
Application.ScreenUpdating = True
End Sub

Function Slicearray(data As Variant, col As Variant) As Variant
With dict
For x = LBound(data, 1) To UBound(data, 1)
  r = 0
  ReDim temp(1 To UBound(col) + 1)
    For Each ele In col
        r = r + 1
        temp(r) = data(x, ele)
    Next
    dict.Item(.Count) = temp
Next
Slicearray = Application.Index(dict.items, 0, 0)
.RemoveAll
End With
End Function
 
Last edited:
Upvote 0
Application.Index has line limiting to use with version 2000 please require modification so the code could work with my database has 8000+ lines
See if this modified version of the code I posted earlier works for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub PatternCounts()
  Dim R As Long, C As Long, X As Long, Z As Long, Cnt As Long
  Dim Pattern As String, Lines(1 To 14) As String
  Dim Data As Variant, Results As Variant
  Data = Range("C6", Cells(Rows.Count, "P").End(xlUp))
  Results = Range("R5", Cells(Cells(Rows.Count, "R").End(xlUp).Row, Cells(5, Columns.Count).End(xlToLeft).Column))
  For R = 2 To UBound(Results, 1)
    For X = 1 To UBound(Data)
      For Z = 1 To 14
        Lines(Z) = Data(X, Z)
      Next
      Pattern = Join(Application.Index(Lines, 1, Split(Results(R, 1), "|")), "|")
      For C = 2 To UBound(Results, 2)
        If Pattern = Results(1, C) Then Results(R, C) = Results(R, C) + 1
      Next
    Next
  Next
  Range("R5").Resize(UBound(Results, 1), UBound(Results, 2)) = Results
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
If above code doesn't work then this will definitely work as I've removed Application.Index which is causing the error in you excel version. Performance is still same :

Code:
Dim x       As Long
Dim c       As Long
Dim ele     As Variant
Dim temp    As Variant
Sub kishan()
Dim i       As Long
Dim n       As Long
Dim k       As Long
Dim Lr      As Long
Dim rng     As Range
Dim nar     As Variant
Dim Pattern As Variant
Dim out     As Variant
Dim ar      As Variant
Dim arr     As Variant
Dim Sp      As Variant

Application.ScreenUpdating = False

Lr = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("S5").Resize(, Cells(5, Columns.Count).End(xlToLeft).Column - 18)
Pattern = Application.Transpose(Application.Transpose(rng))

ReDim out(1 To Lr, 1 To UBound(Pattern))

ar = Range("C6:P" & Lr)
Set rng = Range("R6:R" & Lr)

For Each cell In rng
        n = n + 1
        Sp = Split(cell, "|")
        arr = Slicearray(ar, Sp)
        ReDim nar(1 To UBound(arr, 1))
        
        For i = 1 To UBound(arr)
            nar(i) = Join(Application.Index(arr, i, 0), "|")
            For k = 1 To UBound(Pattern)
                If Pattern(k) = nar(i) Then
                    out(n, k) = out(n, k) + 1
                    Exit For
                End If
            Next
        Next
Next
Range("S6").Resize(n, UBound(out, 2)) = out
Application.ScreenUpdating = True
End Sub

Function Slicearray(data As Variant, col As Variant) As Variant
ReDim temp(1 To UBound(data, 1), 1 To UBound(col) + 1)
For Each ele In col
  c = c + 1
    For x = LBound(data, 1) To UBound(data, 1)
        temp(x, c) = data(x, ele)
    Next
Next
Slicearray = temp
c = 0
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,158
Members
449,208
Latest member
emmac

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