Insert new rows when there is more than 01 item

dandelion

New Member
Joined
Jul 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,

I'm wondering if there is any formula in Excel for VBA macro that can help to insert new rows when there is more thanan item in a specific column. For example, in the picture attached, in column Supplier, the supplier codes are combined and the outome I want is the second table. It means if there is "/" in the cell in Column Supplier, for example: A0C/DEF has 1"/", then there will be 1 row insert below and Row 1: A0C, Row 2: DEF. the remaining value in other cells will be just copied down.

Thanks in advance
 

Attachments

  • Screenshot 2022-11-06 at 9.23.03 AM.png
    Screenshot 2022-11-06 at 9.23.03 AM.png
    222.6 KB · Views: 14

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
VBA Code:
Sub myFunction()
  Dim tArray() As String
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = lRow To 2 Step -1
    tArray = Split(Cells(i, 4).Value, "/")
    If UBound(tArray) > 1 Then
      For ii = 0 to UBound(tArray)-1
        Cells(i, 1).EntireRow.Insert
        Cells(i+ii, 1).Value = Cells(i, 1).Value
        Cells(i+ii, 2).Value = Cells(i, 2).Value
        Cells(i+ii, 3).Value = Cells(i, 3).Value
        Cells(i+ii, 4).Value = tArray(ii)
      Next
    End If
  Next
End Sub
I haven't tested but it should work. I hope :)
 
Upvote 0
VBA Code:
Sub myFunction()
  Dim tArray() As String
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = lRow To 2 Step -1
    tArray = Split(Cells(i, 4).Value, "/")
    If UBound(tArray) > 1 Then
      For ii = 0 to UBound(tArray)-1
        Cells(i, 1).EntireRow.Insert
        Cells(i+ii, 1).Value = Cells(i, 1).Value
        Cells(i+ii, 2).Value = Cells(i, 2).Value
        Cells(i+ii, 3).Value = Cells(i, 3).Value
        Cells(i+ii, 4).Value = tArray(ii)
      Next
    End If
  Next
End Sub
I haven't tested but it should work. I hope :)
Thanks for spending your time, but I tested, nothing happened...
 
Upvote 0
Assuming your data from A to D starting from 2nd row. This function works:
VBA Code:
Sub myFunction()
  Dim tArray() As String
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = lRow To 2 Step -1
    tArray = Split(Cells(i, 4).Value, "/")
    If UBound(tArray) > O Then
      For ii = 0 To UBound(tArray)
        Cells(i + ii+1, 1).EntireRow.Insert
        Cells(i + ii, 1).Value = Cells(i, 1).Value
        Cells(i + ii, 2).Value = Cells(i, 2).Value
        Cells(i + ii, 3).Value = Cells(i, 3).Value
        Cells(i + ii, 4).Value = tArray(ii)
      Next
      Cells(i + ii, 1).EntireRow.Delete
    End If
  Next
End Sub
 
Upvote 0
Solution
Assuming your data from A to D starting from 2nd row. This function works:
VBA Code:
Sub myFunction()
  Dim tArray() As String
  Dim lRow As Integer
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = lRow To 2 Step -1
    tArray = Split(Cells(i, 4).Value, "/")
    If UBound(tArray) > O Then
      For ii = 0 To UBound(tArray)
        Cells(i + ii+1, 1).EntireRow.Insert
        Cells(i + ii, 1).Value = Cells(i, 1).Value
        Cells(i + ii, 2).Value = Cells(i, 2).Value
        Cells(i + ii, 3).Value = Cells(i, 3).Value
        Cells(i + ii, 4).Value = tArray(ii)
      Next
      Cells(i + ii, 1).EntireRow.Delete
    End If
  Next
End Sub
This one works. Thanks a lot :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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