Moving from Sequental Rows to Columns?

petersw

New Member
Joined
Sep 22, 2011
Messages
10
Hi,

Currently have data set up in multiple rows, as follows:

Subject ID
Lot Numbers
10001
9001, 9002
10001
9345, 9100
10002
9004, 9005
10002
9045, 9099

<tbody>
</tbody>

and would like to have it as

Subject ID
Lot Numbers - Grp 1
Lot Number - Grp 2
10001
9001, 9002
9345, 9100
10002
9004, 9005
9045, 9099

<tbody>
</tbody>

I just know this will be easy for someone out there, and thanks in advance!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This can be done with PowerQuery.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Subject ID"}, {{"Count", each _, type table}}),
    Cutom = Table.AddColumn(Group, "Custom", each Table.Column([Count],"Lot Numbers")),
    Extract = Table.TransformColumns(Cutom, {"Custom", each Text.Combine(List.Transform(_, Text.From), "@"), type text}),
    Split = Table.SplitColumn(Extract, "Custom", Splitter.SplitTextByDelimiter("@", QuoteStyle.Csv), {"Lot Numbers - Grp 1", "Lot Numbers - Grp 2"}),
    Remove = Table.RemoveColumns(Split,{"Count"})
in
    Remove
 
Upvote 0
a bit different :)

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Subject ID"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Lot Numbers Grp", each Table.Column([Count],"Lot Numbers")),
    Extract = Table.TransformColumns(List, {"Lot Numbers Grp", each Text.Combine(List.Transform(_, Text.From), "="), type text}),
    Split = Table.SplitColumn(Extract, "Lot Numbers Grp", Splitter.SplitTextByDelimiter("=", QuoteStyle.Csv), {"Lot Numbers Grp.1", "Lot Numbers Grp.2"})
in
    Split[/SIZE]
 
Upvote 0
I keep forgetting that you don't need to remove that unused column with the 'Table' values in them. Good call.
 
Upvote 0
Since I don't know PowerQuery (yet)...I remember I had a macro that did this...I just found it.
Not sure where it came from originally.

I doctored the last few rows to meet your headings.

It works off Sheet1 with your data starting in A1 (row 1 is headers) and assume you have a sheet2 into which the result(s) are place.

Code:
Sub XTranspose()
  Dim OutSH As Worksheet
  Set OutSH = Sheets("Sheet2")
  
  Range("A:A").AdvancedFilter Action:=xlFilterCopy, copytorange:=OutSH.Range("A1"), Unique:=xlYes
  
  'determine last column based on header
  lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
  
  For Each ce In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    Set findit = OutSH.Range("A:A").Find(what:=ce.Value)
    
    For i = 1 To lastcol
      OutSH.Cells(findit.Row, Columns.Count).End(xlToLeft).Offset(0, 1).Value = Cells(ce.Row, i).Value
    Next i
  Next ce
  With OutSH
    .Range(.Range("A2"), .Range("A2").End(xlDown)).Delete shift:=xlToLeft
  End With
  Sheets("Sheet2").Columns("C").Delete
  Sheets("Sheet2").Range("A1") = "Subject ID"
  Sheets("Sheet2").Range("B1") = "Group 1"
  Sheets("Sheet2").Range("C1") = "Group 2"
  
End Sub
 
Upvote 0
A different approach to the VBA solution. This assumes that your original data is in columns A:B, and the output will be in columns D:F.

Code:
Sub GroupIDs()
Application.ScreenUpdating = False


Dim AR() As Variant:    AR = Range("A1:B" & Range("A" & Rows.Count).End(xlUp).Row).Value
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")


For i = LBound(AR) To UBound(AR)
    If Not SD.exists(AR(i, 1)) Then
        SD.Add AR(i, 1), AR(i, 2)
    Else
        SD(AR(i, 1)) = SD(AR(i, 1)) & "@" & AR(i, 2)
    End If
Next i


Range("D1").Resize(SD.Count, 1).Value = Application.Transpose(SD.keys)
Range("E1").Resize(SD.Count, 1).Value = Application.Transpose(SD.items)
Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row).TextToColumns DataType:=xlDelimited, OtherChar:="@"
Range("E1:F1").Value = Array("Lot Numbers - Grp 1", "Lot Numbers - Grp 2")


Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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