Delimited data in two columns, need to combine data between the two columns

kouki

New Member
Joined
May 17, 2022
Messages
1
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi all, first question! I'm hopeful I'll get some tips that will help... Fingers crossed!

My delimted data looks as per the screenshot below - what I have is at the top (rows 1 and 2 in the image), and I what I want to get to is at the bottom (from row 7)

Screen Shot 2022-05-17 at 16.37.37.png


Data are over 20K rows long. The elements aren't always of the same length, as illustrated (hopefully!) in my example above.

I've tried switching text to columns, experimenting with pivot tables, tried playing around with transposing, but no luck and I'm confusing myself.

Any tips would be really greatly appreciated :)
 

Attachments

  • Screen Shot 2022-05-17 at 16.37.37.png
    Screen Shot 2022-05-17 at 16.37.37.png
    28.7 KB · Views: 7

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Being a vba (mostly Access though) person I lean towards a code solution that
- sets lngStartName and lngStartID to 1
- finds the position of the first redirect pipe ( | ) in A2 and assigns to variable lngEndName
- finds the position of the first redirect pipe ( | ) in B2 and assigns to variable lngEndID
- sets start position for Mid function to lngStartName and gets mid of A2 (length is lngEndName); assign to strName
- sets start position for Mid function to lngStartID and gets mid of B2 (length is lngEndID); assign to strID
- adds each resulting string to a 2 dimensional array
- adds lngEndID to lngStartID and lngEndName to lngStartName; repeat from step 2 to find next pipe.
Mid would then use new start positions. When there is no result from trying to find next pipe (because there isn't one), move down one row.

All that is high level and doesn't cover any Trim of spaces, adjustments to the start/end positions because of those spaces, creating/adding/redim of array, determining number of rows, perhaps dealing with mismatched counts of ID's versus Names, etc. etc.
The etc's are things that I can't even foresee.
 
Upvote 0
This works provided the average number of names per cell in the input data doesn't get large enough to require more output rows than available (~ 1E6). Output goes to columns D & E.
For 20,000 rows of data with 3 names per row, on my computer, it takes about 7-8 seconds to run.
VBA Code:
Sub ReorgData()
'assumes raw data in cols A & B with header "Names" in A1
Dim Rnms As Range, Vnms As Variant
Dim i As Long, j As Long, ct As Long, S1 As Variant, S2 As Variant, nxRw As Long
With Range("D:E")
    .ClearContents
    .Cells(1, 1).Resize(1, 2).Value = Array("Name", "ID")
End With
Set Rnms = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
Vnms = Rnms.Value
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
For i = LBound(Vnms, 1) To UBound(Vnms, 1)
    S1 = Split(Vnms(i, 1), " | ")
    S2 = Split(Vnms(i, 2), " | ")
    nxRw = Cells(Rows.Count, "D").End(xlUp).Row + 1
    Range("D" & nxRw).Resize(UBound(S1) + 1).Value = Application.Transpose(S1)
    Range("E" & nxRw).Resize(UBound(S2) + 1).Value = Application.Transpose(S2)
Next i
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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