?
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
Medium Priority
?
314 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 93

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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

765 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