Adding lines and copying the content of each line based on a cell value

scooper62

New Member
Joined
Mar 1, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi Forum,
This is my very first post in the forum and hope someone can help me.
I have no idea writing VBA but am sure there must be a VBA that can resolve my problem.

I have a large Excel file with hundreds of lines.
In column F I have a numerical value that based on this value I want additional lines added below this line.

I think the image that I have attached is self explanatory.
For example row 2 ( marked in yellow) has Qty 3 -- I need then 3 lines with all cells copied from row 2 - see below image.

Anyone here who can help build a VBA that I can simply copy and try it out?

Thanks
Steve
 

Attachments

  • VBA Help needed.jpg
    VBA Help needed.jpg
    142.4 KB · Views: 9

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi
And welcome
Try
VBA Code:
Sub InsertRow()
    Dim x
    Dim lastrow, irow
    lastrow = Range("a2").End(xlDown).Row
    irow = lastrow
    Do Until irow = 1
        x = Cells(irow, 6).Value
        If x > 1 Then
            Rows(irow).EntireRow.Copy
            Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown
            Cells(irow, 6).Resize(x) = 1
        End If
        irow = irow - 1
    Loop
End Sub
 
Upvote 0
Sub InsertRow() Dim x Dim lastrow, irow lastrow = Range("a2").End(xlDown).Row irow = lastrow Do Until irow = 1 x = Cells(irow, 6).Value If x > 1 Then Rows(irow).EntireRow.Copy Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown Cells(irow, 6).Resize(x) = 1 End If irow = irow - 1 Loop End Sub
Works perfect
Thank you so much
 
Upvote 0
Hi
And welcome
Try
VBA Code:
Sub InsertRow()
    Dim x
    Dim lastrow, irow
    lastrow = Range("a2").End(xlDown).Row
    irow = lastrow
    Do Until irow = 1
        x = Cells(irow, 6).Value
        If x > 1 Then
            Rows(irow).EntireRow.Copy
            Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown
            Cells(irow, 6).Resize(x) = 1
        End If
        irow = irow - 1
    Loop
End Sub
Hi Mohadin
You really helped me and I was wondering if the following can also be done in this VBA
To take total value in line, then divide by the QTY and then place the divided figure in each line.
I think what I attached explains much better.
This would be so amazing if possible.

Thank you
 

Attachments

  • Split Lines and calculate value .jpg
    Split Lines and calculate value .jpg
    154.1 KB · Views: 6
Upvote 0
Ok
Working on it
just give me some time Please
thanks
 
Upvote 0
Try
VBA Code:
Sub InsertRow()
    Dim x
    Dim lastrow, irow
    lastrow = Range("a2").End(xlDown).Row
    irow = lastrow
    Do Until irow = 1
        x = Cells(irow, 6).Value
        If x > 1 Then
            Rows(irow).EntireRow.Copy
            Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown
            Cells(irow, 6).Resize(x) = 1
            Cells(irow, 7).Resize(x) = Cells(irow, 7) / x
        End If
        irow = irow - 1
    Loop
End Sub
 
Upvote 0
Sub InsertRow() Dim x Dim lastrow, irow lastrow = Range("a2").End(xlDown).Row irow = lastrow Do Until irow = 1 x = Cells(irow, 6).Value If x > 1 Then Rows(irow).EntireRow.Copy Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown Cells(irow, 6).Resize(x) = 1 Cells(irow, 7).Resize(x) = Cells(irow, 7) / x End If irow = irow - 1 Loop End Sub
You are a champion
Works perfect
Thank you so much
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy and safe
Allow me for final touch
VBA Code:
Sub InsertRow()
    Dim x
    Dim lastrow, irow
    lastrow = Range("a2").End(xlDown).Row
    irow = lastrow
    Application.ScreenUpdating = False
    Do Until irow = 1
        x = Cells(irow, 6).Value
        If x > 1 Then
            Rows(irow).EntireRow.Copy
            Rows(irow + 1).Resize(x - 1).Insert Shift:=xlDown
            Cells(irow, 6).Resize(x) = 1
            Cells(irow, 7).Resize(x) = Cells(irow, 7) / x
        End If
        irow = irow - 1
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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