Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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

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
niceoneishere
Asked:
niceoneishere
  • 2
  • 2
1 Solution
 
Patrick MatthewsCommented:
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
 
niceoneishereAuthor Commented:
Sorry about that and thanks for quick reply, I have attached a new and corrected sample.

Thanks
test-sample.xlsx
0
 
Patrick MatthewsCommented:
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
 
niceoneishereAuthor Commented:
Brilliant. Thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now