Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
322 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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