VBA/Macro to add extra lines from a value

lee2121

New Member
Joined
Mar 14, 2017
Messages
41
Hello Everyone.

I have a file which will contain a different amount of rows and values and i want to be able to click a button (macro) which will look at the numeric value in column L and depending on that value it will create and copy the whole row.

Data before:

PL Tray Labels Week 7.xlsx
ABCDEFGHIJKL
1ABCDEFGHIJKL
2Agapanthus africanus22528202407PL20240101-0163923-002401217Twister40AGA-AF12345 Acorn4
3Kniphofia uvaria22528202407PL20240101-0163924-002452555Poco Citron40KNI-UV56789 Blueberry1
Sheet1


Data result if possible:

PL Tray Labels Week 7.xlsx
ABCDEFGHIJKL
1ABCDEFGHIJKL
2Agapanthus africanus22528202407PL20240101-0163923-002401217Twister40AGA-AF12345 Acorn4
3Agapanthus africanus22528202407PL20240101-0163923-002401217Twister40AGA-AF12345 Acorn4
4Agapanthus africanus22528202407PL20240101-0163923-002401217Twister40AGA-AF12345 Acorn4
5Agapanthus africanus22528202407PL20240101-0163923-002401217Twister40AGA-AF12345 Acorn4
6Kniphofia uvaria22528202407PL20240101-0163924-002452555Poco Citron40KNI-UV56789 Blueberry1
Sheet2
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here is one way:
VBA Code:
Sub MyCopy()

    Dim lr As Long, r As Long, n As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column L with data
    lr = Cells(Rows.Count, "L").End(xlUp).Row
    
'   Loop through rows backwards up to row 2
    For r = lr To 2 Step -1
'       See if value in column L > 1
        n = Cells(r, "L").Value
        If n > 1 Then
'           Insert rows
            Rows(r + 1 & ":" & r + n - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'           Copy values from row down to newly inserted rows
            Rows(r).Copy Rows(r + 1 & ":" & r + n - 1)
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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