Frequency Analysis for all Combinations of Data Across Multiple Columns

sakendrick

New Member
Joined
Mar 21, 2007
Messages
22
I've seen a few solutions for simple versions of the ask on different boards, but haven't found one that solves my need. I'm trying to find the most frequent combination of products owned across all our customers. But these are not unique combinations. For example 80% of my customers may have Product 1. 20 % of my customers may have product 1 and 2... but that includes some of those counted n the first 80%. 10% of my customers may have 1 & 2 and 3, which includes customers counted in the first two frequencies. Hope that makes sense. Added a mini sheet to show what my data looks like and a portion of the expected outcome. Hoping there's a way to use features or code this frequency analysis.

Frequency Analysis.xlsx
DEFGHIJKLMNO
7Product 1Product 2Product 3Product 4Product 5Product 6Product 7
8Cust 111Product 180%
9Cust 21111Product 1 & 220%
10Cust 311Product 1 & 2 & 310%
11Cust 411Product 1 & 333%
12Cust 5111Product 2 & 320%
13Cust 61...So on
14Cust 711...and so on
15Cust 811
16Cust 911
17Cust 1011
188343301
Sheet1
Cell Formulas
RangeFormula
O9O9=20%
E18:K18E18=COUNT(E8:E17)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Let me know if this is what you are looking for.

Book1
ABCDEFGHIJKLMNO
1CustomerProduct 1Product 2Product 3Product 4Product 5Product 6Product 7ComboCountPercent
2Cust 1111880%
3Cust 211112330%
4Cust 3113440%
5Cust 4114330%
6Cust 51115330%
7Cust 617110%
8Cust 7111,2220%
9Cust 8111,3330%
10Cust 9111,4330%
11Cust 10111,5220%
122,3220%
132,5110%
143,4110%
153,5110%
165,7110%
171,2,3110%
181,2,5110%
191,3,4110%
201,3,5110%
212,3,5110%
221,2,3,5110%
Sheet1


VBA Code:
Sub COMBOX()
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim Data() As Variant:  Data = Range("B2:H" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim Grp As Integer:     Grp = 7
Dim Tot As Integer:     Tot = 7
Dim b As Boolean:       b = True
Dim i As Integer
Dim r As Range

For i = 1 To Grp
    Main AL, i, Tot
Next i

For Each AI In AL
    SP = Split(AI, ",")
    For ro = 1 To UBound(Data)
        b = True
        For Each s In SP
            If Data(ro, s) <> 1 Then b = False
        Next s
        If b Then SD(AI) = SD(AI) + 1
    Next ro
Next AI

With Range("M1:O1")
    .Value = Array("Combo", "Count", "Percent")
    .Font.Bold = True
End With
Set r = Range("M2").Resize(SD.Count)
r.Value2 = Application.Transpose(SD.keys)
r.Offset(, 1).Value2 = Application.Transpose(SD.items)
With r.Offset(, 2)
    .Formula2R1C1 = "=RC[-1]/10"
    .Value = .Value2
    .NumberFormat = "#,##0%"
End With

End Sub

Sub Main(AL As Object, Grp As Integer, Tot As Integer)
Dim AR() As Variant:        AR = Evaluate("TRANSPOSE(INDEX(ROW(1:" & Tot & "),))")

Combo AR, Grp, 1, 0, "", AL
End Sub

Sub Combo(AR() As Variant, Grp As Integer, IDX As Integer, Depth As Integer, Buffer As String, AL As Object)
Dim Prefix As String

For i = IDX To UBound(AR)
    If Buffer = vbNullString Then
        Prefix = AR(i)
    Else
        Prefix = Join(Array(Buffer, AR(i)), ",")
    End If
    If Depth + 1 = Grp Then
        AL.Add Prefix
    Else
        Combo AR, Grp, i + 1, Depth + 1, Prefix, AL
    End If
Next i
End Sub
 
Upvote 0
Solution
Hello Kendrick, I don't think it's possible to do that only with formulas.
You can think about new approach. Here is how I see analyse.
If you want to analyze products they should be in the rows and customers in the columns.
After that you should select in the filter products you want to analyse.
With VBA code you can count only visible values (xlVisible) in the each column.
Now you can also with VBA code count columns with same number of values as number of the visible rows.
In the end, this number divide with number of all products in the table and format as percent.
 
Upvote 0
@lrobbo314 the resulting data looks correct - let me see if I can manipulate it for my real data... I"m assuming this goes in code for the page... weird question, but how do I run it? I'm relatively new to using VBA, and normally trigger the routine off of a cell change.
 
Upvote 0
so I tried pasting in my sample file that I shared here, but when I run it I get an automation error... no other information ?‍♂️
 
Upvote 0
Get into the VBA editor and go to insert, then module. Paste the code in the standard module and run it. The data I pasted didn't have the total row like the original did, so maybe get rid of that and try. Give that a shot and get back to me. Automation error sounds like it might be that you are missing .net. but let me know if you still get the error.
 
Upvote 0
yep - I had tried all that - remove the total row, inserted a module and pasted code in there. Seems to choke on the first Set AL=.. Appreciate the help.

1651804824801.png


1651804875908.png
 
Upvote 0
Yeah, that's because it's missing .net. you can try installing it. Otherwise, I can adapt it tomorrow to use a dictionary instead of an arraylist.
 
Upvote 0
I had another system I could test on that I guess had the right .net framework as it works. Now to customize this for a different spreadsheet... what are the items I need to edit?
- I see two integers set to a value of 7... is that the number of columns and should be adjusted?
- I see references to M1:O1 and M2... is that the output location?

anything else?
Thanks again - this is great.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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