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
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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