SOLVED

Consolidate a list based on identical values in one column

%3CLINGO-SUB%20id%3D%22lingo-sub-2359808%22%20slang%3D%22de-DE%22%3EConsolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2359808%22%20slang%3D%22de-DE%22%3E%3CP%3EHello%20together%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20got%20a%20some%20kind%20of%20a%20specific%20issue.%20I%20got%20an%20Excel%20file%20with%202%20columns%2C%20skus%20in%20the%20first%20column%20and%20all%20their%20categories%20in%20the%20second.%20There%20are%20multiple%20links%20for%20every%20sku%20and%20all%20of%20them%20are%20in%20their%20own%20row.%20Unfortunately%20I'm%20not%20able%20to%20import%20the%20file%20with%20this%20layout.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I%20need%20is%20to%20transform%20the%20table%20to%20have%20only%20one%20row%20for%20each%20sku%20and%20all%20category%20links%20in%20the%20columns%20after.%20I%20tried%20the%20consolidate%20function%2C%20but%20the%20only%20result%20is%20that%20I%20got%20the%20number%20of%20category%20links%20for%20every%20sku%20but%20not%20the%20values%20itself.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20give%20you%20a%20better%20understanding%20I%20prepared%20a%20small%20example%20and%20attached%20it%20to%20my%20post.%20In%20the%20sheet%20%22Current%20List%22%20you%20can%20find%20the%20current%20layout%20of%20the%20list%20(the%20original%20has%20800%2C000%20rows)%20and%20in%20the%20sheet%20%22Result%22%20you%20find%20the%20result%20I%20need%20to%20achieve.%20This%20has%20been%20done%20manually%2C%20but%20it's%20impossible%20for%20the%20whole%20file.%3C%2FP%3E%3CP%3EIf%20this%20result%20is%20impossible%20to%20achieve%2C%20it%20is%20also%20fine%20if%20all%20category%20values%20are%20placed%20in%20one%20cell%20comma%20or%20semicolon%20separated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20you%20can%20help%20me%20here%2C%20because%20I%20don't%20have%20any%20idea%20how%20to%20do%20this%20automatically.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3EJ%C3%BCrgen%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2359808%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360076%22%20slang%3D%22en-US%22%3ERe%3A%20Consolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360076%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1055502%22%20target%3D%22_blank%22%3E%40Juergen_Thoma%3C%2FA%3E%26nbsp%3BAdded%20a%20query%20(connecting%20to%20a%20copy%20of%20the%20%22Current%20List%22)%20to%20your%20file%20producing%20the%20desired%20results.%20See%20if%20you%20can%20get%20it%20to%20work%20on%20your%20real%20data.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360237%22%20slang%3D%22en-US%22%3ERe%3A%20Consolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360237%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1055502%22%20target%3D%22_blank%22%3E%40Juergen_Thoma%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20macro%20you%20can%20run%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20Transform()%0A%20%20%20%20Dim%20wshS%20As%20Worksheet%0A%20%20%20%20Dim%20wshT%20As%20Worksheet%0A%20%20%20%20Dim%20s%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20t%20As%20Long%0A%20%20%20%20Dim%20c%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20wshS%20%3D%20Worksheets(%22Current%20List%22)%0A%20%20%20%20Set%20wshT%20%3D%20Worksheets(%22Result%22)%0A%20%20%20%20wshT.Range(%22A2%3AA%22%20%26amp%3B%20wshT.Rows.Count).EntireRow.Clear%0A%20%20%20%20m%20%3D%20wshS.Range(%22A%22%20%26amp%3B%20wshS.Rows.Count).End(xlUp).Row%0A%20%20%20%20t%20%3D%201%0A%20%20%20%20For%20s%20%3D%202%20To%20m%0A%20%20%20%20%20%20%20%20If%20wshS.Cells(s%2C%201).Value%20%26lt%3B%26gt%3B%20wshS.Cells(s%20-%201%2C%201).Value%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20t%20%3D%20t%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20wshT.Cells(t%2C%201).Value%20%3D%20wshS.Cells(s%2C%201).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20c%20%3D%201%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20c%20%3D%20c%20%2B%201%0A%20%20%20%20%20%20%20%20wshT.Cells(t%2C%20c).Value%20%3D%20wshS.Cells(s%2C%202).Value%0A%20%20%20%20Next%20s%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360625%22%20slang%3D%22de-DE%22%3ERe%3A%20Consolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360625%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3Evogelaar%20%40Hans%3C%2FA%3E%26nbsp%3BThx%20for%20the%20macro%2C%20it%20works%20fine%20with%20the%20exception%2C%20that%20leading%20zeros%20in%20the%20values%20will%20be%20removed%2C%20e.g.%200000366%20in%20the%20%22Current%20List%22%20is%20366%20in%20the%20%22Result%22%20table.%20I%20couldn't%20solve%20this%20with%20cell%20formatting%2C%20maybe%20it%20must%20be%20part%20of%20the%20macro%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360640%22%20slang%3D%22en-US%22%3ERe%3A%20Consolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360640%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1055502%22%20target%3D%22_blank%22%3E%40Juergen_Thoma%3C%2FA%3E%26nbsp%3BWhy%20bother%20with%20VBA%20in%20the%20first%20place%3F%20You%20already%20use%20Power%20Query%20to%20connect%20to%20an%20%26nbsp%3BSQL%20database%20with%20799847%20rows.%20Just%20add%20a%20few%20more%20applied%20steps%20and%20you're%20done.%20No%20need%20to%20load%20the%20entire%20table%20in%20an%20Excel%20either.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360644%22%20slang%3D%22en-US%22%3ERe%3A%20Consolidate%20a%20list%20based%20on%20identical%20values%20in%20one%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360644%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1055502%22%20target%3D%22_blank%22%3E%40Juergen_Thoma%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20this%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20Transform()%0A%20%20%20%20Dim%20wshS%20As%20Worksheet%0A%20%20%20%20Dim%20wshT%20As%20Worksheet%0A%20%20%20%20Dim%20s%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20t%20As%20Long%0A%20%20%20%20Dim%20c%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20wshS%20%3D%20Worksheets(%22Current%20List%22)%0A%20%20%20%20Set%20wshT%20%3D%20Worksheets(%22Result%22)%0A%20%20%20%20wshT.Range(%22A2%3AA%22%20%26amp%3B%20wshT.Rows.Count).EntireRow.Clear%0A%20%20%20%20m%20%3D%20wshS.Range(%22A%22%20%26amp%3B%20wshS.Rows.Count).End(xlUp).Row%0A%20%20%20%20t%20%3D%201%0A%20%20%20%20For%20s%20%3D%202%20To%20m%0A%20%20%20%20%20%20%20%20If%20wshS.Cells(s%2C%201).Value%20%26lt%3B%26gt%3B%20wshS.Cells(s%20-%201%2C%201).Value%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20t%20%3D%20t%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20wshT.Cells(t%2C%201).EntireRow.NumberFormat%20%3D%20%22%40%22%0A%20%20%20%20%20%20%20%20%20%20%20%20wshT.Cells(t%2C%201).Value%20%3D%20wshS.Cells(s%2C%201).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20c%20%3D%201%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20c%20%3D%20c%20%2B%201%0A%20%20%20%20%20%20%20%20wshT.Cells(t%2C%20c).Value%20%3D%20wshS.Cells(s%2C%202).Value%0A%20%20%20%20Next%20s%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E(But%20PowerQuery%20might%20well%20be%20better)%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello together,

 

I got a some kind of a specific issue. I got an Excel file with 2 columns, skus in the first column and all their categories in the second. There are multiple links for every sku and all of them are in their own row. Unfortunately I'm not able to import the file with this layout.

 

What I need is to transform the table to have only one row for each sku and all category links in the columns after. I tried the consolidate function, but the only result is that I got the number of category links for every sku but not the values itself.

 

To give you a better understanding I prepared a small example and attached it to my post. In the sheet "Current List" you can find the current layout of the list (the original has 800.000 rows) and in the sheet "Result" you find the result I need to achieve. This has been done manually, but it's impossible for the whole file.

If this result is impossible to achieve, it is also fine if all category values are placed in one cell comma or semicolon separated.

 

I hope you can help me here, because I don't have any idea how to do this automatically.

 

Best regards

Jürgen

6 Replies

@Juergen_Thoma Added a query (connecting to a copy of the "Current List") to your file producing the desired results. See if you can get it to work on your real data.

@Juergen_Thoma 

Here is a macro you can run:

Sub Transform()
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Dim s As Long
    Dim m As Long
    Dim t As Long
    Dim c As Long
    Application.ScreenUpdating = False
    Set wshS = Worksheets("Current List")
    Set wshT = Worksheets("Result")
    wshT.Range("A2:A" & wshT.Rows.Count).EntireRow.Clear
    m = wshS.Range("A" & wshS.Rows.Count).End(xlUp).Row
    t = 1
    For s = 2 To m
        If wshS.Cells(s, 1).Value <> wshS.Cells(s - 1, 1).Value Then
            t = t + 1
            wshT.Cells(t, 1).Value = wshS.Cells(s, 1).Value
            c = 1
        End If
        c = c + 1
        wshT.Cells(t, c).Value = wshS.Cells(s, 2).Value
    Next s
    Application.ScreenUpdating = True
End Sub

@Hans Vogelaar Thx for the macro, it works fine with the exception, that leading zeros in the values will be removed, e.g. 0000366 in the "Current List" is 366 in the "Result" table. I couldn't solve this with cell formatting, maybe it must be part of the macro?

@Juergen_Thoma Why bother with VBA in the first place? You already use Power Query to connect to an  SQL database with 799847 rows. Just add a few more applied steps and you're done. No need to load the entire table in an Excel either.

best response confirmed by Juergen_Thoma (New Contributor)
Solution

@Juergen_Thoma 

Like this:

Sub Transform()
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Dim s As Long
    Dim m As Long
    Dim t As Long
    Dim c As Long
    Application.ScreenUpdating = False
    Set wshS = Worksheets("Current List")
    Set wshT = Worksheets("Result")
    wshT.Range("A2:A" & wshT.Rows.Count).EntireRow.Clear
    m = wshS.Range("A" & wshS.Rows.Count).End(xlUp).Row
    t = 1
    For s = 2 To m
        If wshS.Cells(s, 1).Value <> wshS.Cells(s - 1, 1).Value Then
            t = t + 1
            wshT.Cells(t, 1).EntireRow.NumberFormat = "@"
            wshT.Cells(t, 1).Value = wshS.Cells(s, 1).Value
            c = 1
        End If
        c = c + 1
        wshT.Cells(t, c).Value = wshS.Cells(s, 2).Value
    Next s
    Application.ScreenUpdating = True
End Sub

(But PowerQuery might well be better)

@Hans Vogelaar @Riny_van_Eekelen 

Hello together,

 

sorry for the late response. I just wanted to say that both solutions worked fine for me, thanks a lot for your feedback. In the end changing the query was the more efficient way, bacause I didn't have to load the entire table in Excel.

 

Best regards

Jürgen