Excel macro -Split semicolon separated entries to new rows

AT BABU

Board Regular
Joined
Oct 12, 2018
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
I currently have this data in a sheet

Sl NoDateReviewed byAssignmentPole NumberFeature ClassAttributeError CategoryObservation
18/2/2019Bob.BBatch1101039656Cross-ArmRotationAttributeArm is reversed, causing primaries to cross; Delete riser.
28/2/2019Bob.BBatch1101039656Cross-ArmRotationAttributeArm is reversed, causing primaries to cross; Delete riser.

What I want to do is split the semicolon separated entries in the third column and insert in new rows like below:

Sl NoDateReviewed byAssignmentPole NumberFeature ClassAttributeError CategoryObservation
18/2/2019Bob.BBatch1101039656Cross-ArmRotationAttributeArm is reversed, causing primaries to cross;
28/2/2019Bob.BBatch1101039656Cross-ArmRotationAttributeDelete riser.
38/2/2019Bob.BBatch1101039656Cross-ArmRotationAttributeArm is reversed, causing primaries to cross;
48/2/2019Bob.BBatch1101039656Cross-ArmRotationAttribute Delete riser.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about
VBA Code:
Sub atbabu()
    Dim i As Long, j As Long
    Dim Sp As Variant
    
    For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        Sp = Split(Cells(i, 9), "; ")
        If UBound(Sp) > 0 Then
            Rows(i + 1).Insert
            Rows(i).Resize(2).FillDown
            For j = 0 To UBound(Sp)
                Cells(i + j, 9) = Sp(j)
            Next j
        End If
    Next i
End Sub
 
Upvote 0
Just an option: Instead of looping through Sp and entering the values individually, you could enter them all at once.

Also, I assume that there could be more than 2 values separated by semicolons in column I so the number of rows inserted & the resize would need to be variable.

VBA Code:
Sub atbabu_v2()
    Dim i As Long, j As Long, rws As Long
    Dim Sp As Variant
    
    For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        Sp = Split(Cells(i, 9), "; ")
        rws = UBound(Sp) + 1
        If rws > 0 Then
            Rows(i + 1).Resize(rws - 1).Insert
            Rows(i).Resize(rws).FillDown
            Cells(i, 9).Resize(rws).Value = Application.Transpose(Sp)
        End If
    Next i
End Sub
 
Upvote 0
I assume that there could be more than 2 values separated by semicolons in column I so the number of rows inserted & the resize would need to be variable.

:oops: Not enough coffee yet :(
 
Upvote 0
Hi Peter

Thank you for your support

when I run the code i get error

Just an option: Instead of looping through Sp and entering the values individually, you could enter them all at once.
Yes
1576759477510.png

original data like that
1576759582169.png
 
Upvote 0

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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