Solved

MS Excel 2007 loop thru based on sku no and display size and color in the next columns

Posted on 2014-04-22
4
298 Views
Last Modified: 2014-04-24
My current excel 2007 sheet has data like below. I have attached a sample file

Sku       Color   Size
1001    Black   S
1001   Black   L
1001   Red    S
1001   Red   XL
1002   Black  XXL
1002   Yellow XS
1003D  Tan  XL
1003D  Tan XXL

I am trying to display them as follows

Sku      Color            Size
1001   Black,Red    S-L-XL
1002  Black,Yellow  XS-XXL

is this possible to do this using Excel.

Thanks and appreciate it
test-sample.xlsx
0
Comment
Question by:niceoneishere
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 40015930
Your source data are not consistent.  Check rows 122-139 in your sample file.

You need to either post a new sample file that is consistent, or you need to tell us how to handle those rows (and whether there are any other anomalies, too).
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 40015940
Sorry about that and thanks for quick reply, I have attached a new and corrected sample.

Thanks
test-sample.xlsx
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 40016153
This appears to be working.

1) Add this to a regular VBA module:

Sub MakeList()
    
    Dim SkuDic As Object
    Dim ItemDic As Object
    Dim ColorDic As Object
    Dim SizeDic As Object
    Dim LastR As Long
    Dim Counter As Long
    Dim arr As Variant
    Dim xItem As String
    Dim xColor As String
    Dim xSize As String
    Dim Sku As Variant, Skus As Variant
    
    With ActiveSheet
        LastR = .Cells(.Rows.Count, "a").End(xlUp).Row
        arr = .Range("a1:c" & LastR).Value
    End With
    
    Worksheets.Add
    
    [a1:c1].Value = Array("SKU", "Color", "Size")
    
    Set SkuDic = CreateObject("Scripting.Dictionary")
    SkuDic.CompareMode = vbTextCompare
    
    For Counter = 2 To LastR
        xItem = arr(Counter, 1)
        xColor = arr(Counter, 2)
        xSize = arr(Counter, 3)
        If SkuDic.Exists(xItem) Then
            Set ItemDic = SkuDic.Item(xItem)
            Set ColorDic = ItemDic.Item("Color")
            Set SizeDic = ItemDic.Item("Size")
        Else
            Set ItemDic = CreateObject("Scripting.Dictionary")
            SkuDic.Add xItem, ItemDic
            Set ColorDic = CreateObject("Scripting.Dictionary")
            ItemDic.Add "Color", ColorDic
            Set SizeDic = CreateObject("Scripting.Dictionary")
            ItemDic.Add "Size", SizeDic
        End If
        If Not ColorDic.Exists(xColor) Then ColorDic.Add xColor, xColor
        If Not SizeDic.Exists(xSize) Then SizeDic.Add xSize, xSize
    Next
    
    Skus = SkuDic.Keys
    Counter = 2
    
    For Each Sku In Skus
        Cells(Counter, 1) = Sku
        Cells(Counter, 2) = Join(SkuDic.Item(Sku).Item("Color").Keys, ", ")
        Cells(Counter, 3) = Join(SkuDic.Item(Sku).Item("Size").Keys, "-")
        Counter = Counter + 1
    Next
    
    Set ColorDic = Nothing
    Set SizeDic = Nothing
    Set SkuDic = Nothing
    Set ItemDic = Nothing
    
    Columns.AutoFit
    
End Sub

Open in new window


Make sure your data are on the active worksheet, and run that macro.

It uses a "dictionary of dictionaries" approach as I outlined in my article here: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
0
 
LVL 1

Author Closing Comment

by:niceoneishere
ID: 40020633
Brilliant. Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now