Merge rows keeping all data

inactiveUser333473

New Member
Joined
Jul 10, 2015
Messages
45
Hi, I have searched for this elsewhere but cannot find a solved solution.

In table one I have the data as it would be, in table two I have the data as I would want it.

Can anyone help?

Name Surname Org Title Team 1 Skill 1 Skill 2 Skill 3 Skill 4 Skill 5 Skill 6 Skill 7 Skill 8 Gender In Contract
Terry Wogan BBC Host Team 1 L2 m
Terry Wogan BBC Host Team 1 Res m
Terry Wogan BBC Host Team 1 Cdr m
Terry Wogan BBC Host Team 1 Bas m
Terry Wogan BBC Host Team 1 Adv m

Name Surname Org Title Team 1 Skill 1 Skill 2 Skill 3 Skill 4 Skill 5 Skill 6 Skill 7 Skill 8 Gender In Contract
Terry Wogan BBC Host Team 1 Adv Res Cdr Bas L2 m
 
I have run your code, but it didn't do anything, do I need to highlight rows that I want to merge prior to running?
No, you don't need to highlight or select anything.
The sheet just needs to be the active sheet and the code was written for data in columns starting at column A and headings in row 1. Is that what you have?
Did you look down below your data as that is where the code currently puts the results. I will change that if the code ends up at least getting the results you want.
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Version: File - Account
Data - section: Get&Transform (changed from Power Query)

representative example (or link to example excel file) is very helpful especially for PowerQuery
 
Upvote 0
I added dummy data, so

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]Name[/td][td=bgcolor:#5B9BD5]Surname[/td][td=bgcolor:#5B9BD5]Org[/td][td=bgcolor:#5B9BD5]Title[/td][td=bgcolor:#5B9BD5]Team 1[/td][td=bgcolor:#5B9BD5]Skill 1[/td][td=bgcolor:#5B9BD5]Skill 2[/td][td=bgcolor:#5B9BD5]Skill 3[/td][td=bgcolor:#5B9BD5]Skill 4[/td][td=bgcolor:#5B9BD5]Skill 5[/td][td=bgcolor:#5B9BD5]Skill 6[/td][td=bgcolor:#5B9BD5]Skill 7[/td][td=bgcolor:#5B9BD5]Skill 8[/td][td=bgcolor:#5B9BD5]Gender[/td][td=bgcolor:#5B9BD5]In Contract[/td][td][/td][td=bgcolor:#70AD47]Name[/td][td=bgcolor:#70AD47]Surname[/td][td=bgcolor:#70AD47]Org[/td][td=bgcolor:#70AD47]Title[/td][td=bgcolor:#70AD47]Team 1[/td][td=bgcolor:#70AD47]Skill 1[/td][td=bgcolor:#70AD47]Skill 2[/td][td=bgcolor:#70AD47]Skill 3[/td][td=bgcolor:#70AD47]Skill 4[/td][td=bgcolor:#70AD47]Skill 5[/td][td=bgcolor:#70AD47]Skill 6[/td][td=bgcolor:#70AD47]Skill 7[/td][td=bgcolor:#70AD47]Skill 8[/td][td=bgcolor:#70AD47]Gender[/td][td=bgcolor:#70AD47]In Contract[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Terry[/td][td=bgcolor:#DDEBF7]Wogan[/td][td=bgcolor:#DDEBF7]BBC[/td][td=bgcolor:#DDEBF7]Host[/td][td=bgcolor:#DDEBF7]Team 1[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]L2[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]m[/td][td=bgcolor:#DDEBF7][/td][td][/td][td=bgcolor:#E2EFDA]Terry[/td][td=bgcolor:#E2EFDA]Wogan[/td][td=bgcolor:#E2EFDA]BBC[/td][td=bgcolor:#E2EFDA]Host[/td][td=bgcolor:#E2EFDA]Team 1[/td][td=bgcolor:#E2EFDA]Adv[/td][td=bgcolor:#E2EFDA]Res[/td][td=bgcolor:#E2EFDA]Cdr[/td][td=bgcolor:#E2EFDA]Bas[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]L2[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]m[/td][td=bgcolor:#E2EFDA][/td][/tr]

[tr=bgcolor:#FFFFFF][td]Terry[/td][td]Wogan[/td][td]BBC[/td][td]Host[/td][td]Team 1[/td][td][/td][td]Res[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]m[/td][td][/td][td][/td][td]Jim[/td][td]Look[/td][td]NBC[/td][td]Host[/td][td]Team 2[/td][td]Adv[/td][td]Res[/td][td]Cdr[/td][td]Bas[/td][td][/td][td]L2[/td][td][/td][td][/td][td]m[/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Terry[/td][td=bgcolor:#DDEBF7]Wogan[/td][td=bgcolor:#DDEBF7]BBC[/td][td=bgcolor:#DDEBF7]Host[/td][td=bgcolor:#DDEBF7]Team 1[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]Cdr[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]m[/td][td=bgcolor:#DDEBF7][/td][td][/td][td=bgcolor:#E2EFDA]Mike[/td][td=bgcolor:#E2EFDA]Duran[/td][td=bgcolor:#E2EFDA]CNBC[/td][td=bgcolor:#E2EFDA]Host[/td][td=bgcolor:#E2EFDA]Team 3[/td][td=bgcolor:#E2EFDA]Adv[/td][td=bgcolor:#E2EFDA]REs[/td][td=bgcolor:#E2EFDA]Cdr[/td][td=bgcolor:#E2EFDA]Bas[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]L2[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]x[/td][td=bgcolor:#E2EFDA][/td][/tr]

[tr=bgcolor:#FFFFFF][td]Terry[/td][td]Wogan[/td][td]BBC[/td][td]Host[/td][td]Team 1[/td][td][/td][td][/td][td][/td][td]Bas[/td][td][/td][td][/td][td][/td][td][/td][td]m[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Terry[/td][td=bgcolor:#DDEBF7]Wogan[/td][td=bgcolor:#DDEBF7]BBC[/td][td=bgcolor:#DDEBF7]Host[/td][td=bgcolor:#DDEBF7]Team 1[/td][td=bgcolor:#DDEBF7]Adv[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]m[/td][td=bgcolor:#DDEBF7][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]Jim[/td][td]Look[/td][td]NBC[/td][td]Host[/td][td]Team 2[/td][td]Adv[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]m[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Jim[/td][td=bgcolor:#DDEBF7]Look[/td][td=bgcolor:#DDEBF7]NBC[/td][td=bgcolor:#DDEBF7]Host[/td][td=bgcolor:#DDEBF7]Team 2[/td][td=bgcolor:#DDEBF7]Adv[/td][td=bgcolor:#DDEBF7]Res[/td][td=bgcolor:#DDEBF7]Cdr[/td][td=bgcolor:#DDEBF7]Bas[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]L2[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]m[/td][td=bgcolor:#DDEBF7][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]Mike[/td][td]Duran[/td][td]CNBC[/td][td]Host[/td][td]Team 3[/td][td][/td][td][/td][td]Cdr[/td][td]Bas[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Mike[/td][td=bgcolor:#DDEBF7]Duran[/td][td=bgcolor:#DDEBF7]CNBC[/td][td=bgcolor:#DDEBF7]Host[/td][td=bgcolor:#DDEBF7]Team 3[/td][td=bgcolor:#DDEBF7]Adv[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]Bas[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]L2[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]x[/td][td=bgcolor:#DDEBF7][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]Mike[/td][td]Duran[/td][td]CNBC[/td][td]Host[/td][td]Team 3[/td][td][/td][td]REs[/td][td][/td][td]Bas[/td][td][/td][td]L2[/td][td][/td][td][/td][td]x[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Surname", type text}, {"Org", type text}, {"Title", type text}, {"Team 1", type text}, {"Skill 1", type text}, {"Skill 2", type text}, {"Skill 3", type text}, {"Skill 4", type text}, {"Skill 5", type any}, {"Skill 6", type text}, {"Skill 7", type any}, {"Skill 8", type any}, {"Gender", type text}, {"In Contract", type any}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "Surname", "Org", "Title", "Team 1"}, {{"Count", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Skill 1", each List.Distinct(Table.Column([Count],"Skill 1"))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Skill 1", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom1" = Table.AddColumn(#"Extracted Values", "Skill 2", each List.Distinct(Table.Column([Count],"Skill 2"))),
    #"Extracted Values1" = Table.TransformColumns(#"Added Custom1", {"Skill 2", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom2" = Table.AddColumn(#"Extracted Values1", "Skill 3", each List.Distinct(Table.Column([Count],"Skill 3"))),
    #"Extracted Values2" = Table.TransformColumns(#"Added Custom2", {"Skill 3", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom3" = Table.AddColumn(#"Extracted Values2", "Skill 4", each List.Distinct(Table.Column([Count],"Skill 4"))),
    #"Extracted Values3" = Table.TransformColumns(#"Added Custom3", {"Skill 4", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom4" = Table.AddColumn(#"Extracted Values3", "Skill 5", each List.Distinct(Table.Column([Count],"Skill 5"))),
    #"Extracted Values4" = Table.TransformColumns(#"Added Custom4", {"Skill 5", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom5" = Table.AddColumn(#"Extracted Values4", "Skill 6", each List.Distinct(Table.Column([Count],"Skill 6"))),
    #"Extracted Values5" = Table.TransformColumns(#"Added Custom5", {"Skill 6", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom6" = Table.AddColumn(#"Extracted Values5", "Skill 7", each List.Distinct(Table.Column([Count],"Skill 7"))),
    #"Extracted Values6" = Table.TransformColumns(#"Added Custom6", {"Skill 7", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom7" = Table.AddColumn(#"Extracted Values6", "Skill 8", each List.Distinct(Table.Column([Count],"Skill 8"))),
    #"Extracted Values7" = Table.TransformColumns(#"Added Custom7", {"Skill 8", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom8" = Table.AddColumn(#"Extracted Values7", "Gender", each List.Distinct(Table.Column([Count],"Gender"))),
    #"Extracted Values8" = Table.TransformColumns(#"Added Custom8", {"Gender", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
    #"Added Custom9" = Table.AddColumn(#"Extracted Values8", "In Contract", each List.Distinct(Table.Column([Count],"In Contract"))),
    #"Extracted Values9" = Table.TransformColumns(#"Added Custom9", {"In Contract", each Text.Combine(List.Transform(_, Text.From), " "), type text})
in
    #"Extracted Values9"

example file
 
Last edited:
Upvote 0
Peter, if I could trouble for one last detail, is it possible to output the results on to a new worksheet as values? The original data is made up of multiple formulas that pull the data from various sources...
 
Upvote 0
Hi Peter, I just needed to change it to A2 and it works perfectly, I am very grateful for your time, thanks
So your headings are in row 2 then? In that case I would not use CurrentRegion & have altered that part below too. Also some other minor tweaks to the code.

.. is it possible to output the results on to a new worksheet as values?
Try this version
Code:
Sub MergeToOneRow_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  With Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, Cells(2, Columns.Count).End(xlToLeft).Column)
    a = .Value
    uba2 = UBound(a, 2)
    ReDim b(1 To UBound(a, 1), 1 To uba2)
    For i = 2 To UBound(a)
      If Join(Application.Index(a, i, Array(1, 2, 3, 4, 5)), "|") <> Join(Application.Index(a, i - 1, Array(1, 2, 3, 4, 5)), "|") Then
        k = k + 1
        For j = 1 To 5
          b(k, j) = a(i, j)
        Next j
      End If
      For j = 6 To uba2
        If Len(a(i, j)) > 0 Then b(k, j) = a(i, j)
      Next j
    Next i
    Application.ScreenUpdating = False
    Sheets.Add After:=ActiveSheet
    Range("A2").Resize(k, UBound(b, 2)).Value = b
    Range("A1").Resize(, .Columns.Count).Value = .Rows(1).Value
    Range("A1").CurrentRegion.Columns.AutoFit
    Application.ScreenUpdating = True
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top