Generate related lists of products that have similar attributes

camspy

New Member
Joined
Jan 7, 2022
Messages
43
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
We have a list of products in column A.
Each product has its set of attributes in column B.

The task is to generate a product with its 6 related products (in column C), which have similar attributes to that product, sorted by similarity.

Similar attributes mean that the products share a similar set of attributes. The more matching attributes, the more similar the product is.

Then, the most similar product goes to the top of the list, and the least similar product goes to the bottom.

The result should exclude the product for which we generated the list.

I provide sample data below.
In C2, I have put an example of what the result should look like.

sort.xlsx
ABC
1ProductAtributesResult: 6 related products
2Applepackaging,quantity,reliability,size,design,material,id,contentPear,Orange,Lemon,Fig,Lime,Lychee
3Apricotmake,content,brand,purpose,feel,color,id,photos,weight,packaging
4Avocadoreliability,size,design,taste,smell,feel
5Bananaquantity,design,safety,brand,smell,id,feel,photos
6Duriansize,color,content,feel,smell,quantity,brand,price,reliability
7Figpackaging,safety,weight,id,videos,photos
8Grapefruitweight,purpose,photos,packaging,material,make,color,content,videos,taste
9Grapesquantity,reliability,size,make,material,purpose,content,price
10Lemonpackaging,reliability,design,weight,smell,photos
11Limepackaging,quantity,weight,make,taste,material,price,feel
12Lycheeweight,content,taste,reliability,design,material,size,price,purpose,feel
13Mangotaste,material,purpose,feel,videos,photos
14Nectarinereliability,size,safety,make,material,content,color,photos
15Orangepackaging,quantity,reliability,make,purpose,smell,videos,photos
16Papayasize,design,weight,purpose,id,content
17Peachdesign,safety,content,price,feel,photos,price
18Pearpackaging,design,safety,taste,purpose,price
19Pineapplesafety,taste,content,price,feel,photos
20Plumreliability,size,make,material,content,color,price,photos
21Watermelonpackaging,quantity,safety,brand,make,taste,material,content
Sheet1



Thanks in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Here is the code foe UDF
VBA Code:
Function GetRelatedProducts(RngPrd As Range, Rng As Range, CriPrd As String, CriAtb As String)
Dim M, N, temp
Dim Ta As Long, T As Long, K As Long, Cnt As Long
M = Split(CriAtb, ",")

Cnt = Rng.Cells.Count
ReDim Tly(1 To Cnt) As Long
With CreateObject("scripting.dictionary")

For Ta = 1 To Cnt
For T = 0 To UBound(M)
Tly(Ta) = Tly(Ta) + Evaluate("1*ISnumber(FIND(""" & M(T) & """," & Rng.Cells(Ta, 1).Address & "))")
Next T
.Add Tly(Ta) * 1000 - Ta, RngPrd.Cells(Ta, 1)
Next Ta

For Ta = 1 To Cnt
K = Application.Large(.keys, Ta)
If K > 0 And .Item(K) <> CriPrd Then
temp = temp & "," & .Item(K)
End If
Next Ta
End With
If temp = "" Then GetRelatedProducts = "" Else GetRelatedProducts = Mid(temp, 2)
End Function
Formula in C2 then copied down
Excel Formula:
=GetRelatedProducts($A$2:$A$9,$B$2:$B$9,$A2,$B2)

How to Use UDF code:
In the developer tab click--> Visual Basic
VB window opens
Insert--> Module
Paste the code.
Close the VB window.
Now UDF is available in Function List
 
Upvote 0
@kvsrinivasamurthy Thanks for your help.

How do I specify the number of desired related results?
I now get lots of them, while I only need 6 for each product.
 
Upvote 0
Revised code for UDF.
VBA Code:
Function GetRelatedProducts(RngPrd As Range, Rng As Range, CriPrd As String, CriAtb As String)
Dim M, N, temp
Dim Ta As Long, T As Long, K As Long, Cnt As Long
M = Split(CriAtb, ",")

Cnt = Rng.Cells.Count
ReDim Tly(1 To Cnt) As Long
With CreateObject("scripting.dictionary")

For Ta = 1 To Cnt
For T = 0 To UBound(M)
Tly(Ta) = Tly(Ta) + Evaluate("1*ISnumber(FIND(""" & M(T) & """," & Rng.Cells(Ta, 1).Address & "))")
Next T
.Add Tly(Ta) * 1000 - Ta, RngPrd.Cells(Ta, 1)
Next Ta

For Ta = 1 To WorksheetFunction.Min(7, Cnt)
K = Application.Large(.keys, Ta)
If K > 0 And .Item(K) <> CriPrd Then
temp = temp & "," & .Item(K)
End If
Next Ta
End With
If temp = "" Then GetRelatedProducts = "" Else GetRelatedProducts = Mid(temp, 2)
End Function
 
Last edited:
Upvote 0
Revised code for UDF.
VBA Code:
Function GetRelatedProducts(RngPrd As Range, Rng As Range, CriPrd As String, CriAtb As String)
Dim M, N, temp
Dim Ta As Long, T As Long, K As Long, Cnt As Long
M = Split(CriAtb, ",")

Cnt = Rng.Cells.Count
ReDim Tly(1 To Cnt) As Long
With CreateObject("scripting.dictionary")

For Ta = 1 To Cnt
For T = 0 To UBound(M)
Tly(Ta) = Tly(Ta) + Evaluate("1*ISnumber(FIND(""" & M(T) & """," & Rng.Cells(Ta, 1).Address & "))")
Next T
.Add Tly(Ta) * 1000 - Ta, RngPrd.Cells(Ta, 1)
Next Ta

For Ta = 1 To WorksheetFunction.Min(7, Cnt)
K = Application.Large(.keys, Ta)
If K > 0 And .Item(K) <> CriPrd Then
temp = temp & "," & .Item(K)
End If
Next Ta
End With
If temp = "" Then GetRelatedProducts = "" Else GetRelatedProducts = Mid(temp, 2)
End Function
Thank you for your time and effort!

I had to check how well the sorting from the most similar product to the least similar product works.
I've did a test with 200+ products that have up to 50 attributes. I picked one product and analyzed the results.
The related products for it were sorted this way: 17,17,11,5,15,15.
Those are the numbers of matching attributes.

I would have expected the sorting to be like this: 17,17,15,15,11,5.
 
Upvote 0
Try the UDF =CPcount(Product,Range_of_Products,Number_of_common)
=CPcount(A2,$B$2:$B$21,6)
VBA Code:
Option Explicit
Function CPcount(ByVal target As Range, ByVal rng As Range, ByVal N As Long)
Dim i&, j&, k&, Lr&, max&, count&, cell As Range
Dim st1, st2 As String, arr(), pr
Lr = rng.Rows.count
ReDim arr(1 To Lr, 1 To 2)
st1 = Split(target.Offset(0, 1), ",")
     For Each cell In rng.Offset(0, -1)
     If cell.Value <> target.Value Then
        st2 = "," & cell.Offset(, 1).Value & ","
        For j = 0 To UBound(st1)
            If InStr(1, st2, "," & st1(j) & ",") > 0 Then count = count + 1
        Next
        k = k + 1
        arr(k, 1) = count
        arr(k, 2) = cell
        st2 = ""
        count = 0
    End If
    Next
    For i = 1 To Lr - 1
        For j = i + 1 To Lr
        If arr(j, 1) > arr(i, 1) Then
            max = arr(j, 1)
            pr = arr(j, 2)
            arr(j, 1) = arr(i, 1): arr(j, 2) = arr(i, 2)
            arr(i, 1) = max: arr(i, 2) = pr
        End If
        Next
    Next
st2 = ""
For i = 1 To N
st2 = st2 & ", " & arr(i, 2) & "(" & arr(i, 1) & ")" ' product and count. If product only, use: st2 = st2 & ", " & arr(i, 2)
Next
CPcount = Right(st2, Len(st2) - 1)
End Function
Note that the output like: " Grapes(5), Lychee(5), Durian(4), Nectarine(4), Papaya(4), Plum(4)"
If the output likes: " Grapes, Lychee, Durian, Nectarine, Papaya, Plum", the 4th line from bottom should be:
st2 = st2 & ", " & arr(i, 2)
 
Upvote 0
Solution
Try the UDF =CPcount(Product,Range_of_Products,Number_of_common)
=CPcount(A2,$B$2:$B$21,6)
VBA Code:
Option Explicit
Function CPcount(ByVal target As Range, ByVal rng As Range, ByVal N As Long)
Dim i&, j&, k&, Lr&, max&, count&, cell As Range
Dim st1, st2 As String, arr(), pr
Lr = rng.Rows.count
ReDim arr(1 To Lr, 1 To 2)
st1 = Split(target.Offset(0, 1), ",")
     For Each cell In rng.Offset(0, -1)
     If cell.Value <> target.Value Then
        st2 = "," & cell.Offset(, 1).Value & ","
        For j = 0 To UBound(st1)
            If InStr(1, st2, "," & st1(j) & ",") > 0 Then count = count + 1
        Next
        k = k + 1
        arr(k, 1) = count
        arr(k, 2) = cell
        st2 = ""
        count = 0
    End If
    Next
    For i = 1 To Lr - 1
        For j = i + 1 To Lr
        If arr(j, 1) > arr(i, 1) Then
            max = arr(j, 1)
            pr = arr(j, 2)
            arr(j, 1) = arr(i, 1): arr(j, 2) = arr(i, 2)
            arr(i, 1) = max: arr(i, 2) = pr
        End If
        Next
    Next
st2 = ""
For i = 1 To N
st2 = st2 & ", " & arr(i, 2) & "(" & arr(i, 1) & ")" ' product and count. If product only, use: st2 = st2 & ", " & arr(i, 2)
Next
CPcount = Right(st2, Len(st2) - 1)
End Function
Note that the output like: " Grapes(5), Lychee(5), Durian(4), Nectarine(4), Papaya(4), Plum(4)"
If the output likes: " Grapes, Lychee, Durian, Nectarine, Papaya, Plum", the 4th line from bottom should be:
st2 = st2 & ", " & arr(i, 2)
I've tried both variants, but I get #NAME?
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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