Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a single cell at line breaks

Tags:

excel

vba

I have a spreadsheet that has data in a single cell separated by line breaks. I need to split the cell into separate rows so that I can remove some data and recombine. Like this:

Item Status
285T1150-3 285T0680-1 1 Complete

285T1145-7//D 1 ATS-182

285T1146-1//D 1 Complete

363A4872P4 1 No Router

Convert to this:

Item Status
285T1150-3 285T0680-1 1 Complete
285T1150-3 285T1145-7//D 1 ATS-182
285T1150-3 285T1146-1//D 1 Complete
285T1150-3 363A4872P4 1 No Router

This is the code I've been using:

        check_col = colArray(0)
        ColLastRow = Range(check_col & Rows.Count).End(xlUp).Row
        For Each Rng In Range(check_col & "1" & ":" & check_col & ColLastRow)
            If InStr(Rng.Value, vbLf) Then
                Rng.EntireRow.Copy
                Rng.EntireRow.Insert
                
                For i = 0 To UBound(colArray)
                    c = colArray(i)
                    
                    Set currentrng = Range(c & Rng.Row)
                    Set upperRng = currentrng.Offset(-1, 0)
                
                    upperRng.Value = Mid(currentrng.Value, 1, InStr(currentrng.Value, vbLf) - 1)
                    currentrng.Value = Mid(currentrng.Value, Len(upperRng.Value) + 2, Len(currentrng.Value))
                Next i
            End If
        Next

Which works perfectly. It just takes a very long time. Sometimes upwards of 5-8 minutes. Is there a way I can streamline this so that it runs a little faster?

like image 664
Myykro Avatar asked Aug 27 '26 13:08

Myykro


1 Answers

I'd strongly recommend PowerQuery for the task at hand:

  • Imagine the following sample data;

    enter image description here

  • Select data, then load it into PQ as per below GIF;

    enter image description here

  • PQ will open, follow the steps as below GIF;

    enter image description here

  • Close PQ and save changes. Result will look like:

    enter image description here

like image 178
JvdV Avatar answered Aug 30 '26 07:08

JvdV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!