I have a spreadsheet with 3 columns. Column A contains product type names with the range named as "Product".Column B is a description of the product names and Column C contains supplier names with the range named as "supplier".
I'm using a macro based on a Dictionary object to extract a list of unique supplier names from column C but now wish to modify it so that I can extract a unique list of suppliers based on the Product type. In other words, if product type is "some product", show me a list of its unique suppliers. A sample of the code is below. It's based on a reply to a related question on the Stackoverflow forum by user Issun (thanks).
I hope I've explained clearly. Thank you in advance for your help!
I'm using a macro based on a Dictionary object to extract a list of unique supplier names from column C but now wish to modify it so that I can extract a unique list of suppliers based on the Product type. In other words, if product type is "some product", show me a list of its unique suppliers. A sample of the code is below. It's based on a reply to a related question on the Stackoverflow forum by user Issun (thanks).
I hope I've explained clearly. Thank you in advance for your help!
Code:
Sub UniqueRep()
Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")
Dim var1 As Variant, element As Variant
var1 = Range("supplier").Value
'Generate unique list and count occurrences
For Each element In var1
If dict.1exists(element) Then
dict1.Item(element) = dict1.Item(element) + 1
Else
dict1.Add element, 1
End If
Next
'Paste report data
Sheet2.Range("A1").Resize(dict1.Count, 1).Value = _
WorksheetFunction.Transpose(dict1.keys)
Sheet2.Range("B1").Resize(dict1.Count, 1).Value = _
WorksheetFunction.Transpose(dict1.items)
End Sub
Last edited by a moderator: