Add different number of rows based on values in certain column

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
Hello, I need to add one additional row after any row contain the keyword "Skimmer(2)" in column K, and copy all values from the row with the keyword;
two additional row after any row contain the keyword "Skimmer(3)" in column K, and copy all values from the row with the keyword into the new rows;
three additional row after any row contain the keyword "Skimmer(4)" in column K, and copy all values from the row with the keyword into the new rows

I will run this macro on many files, all files have the same format, see below:
1666641306536.png


any help is greatly appreciated !
 

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 Skimmer()
    lr = Range("K" & Rows.Count).End(xlUp).Row
    
    For i = lr To 1 Step -1
        If Range("K" & i).Value = "Skimmer(2)" Then
            Rows(i + 1 & ":" & i + 1).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 1)
        End If
        If Range("K" & i).Value = "Skimmer(3)" Then
            Rows(i + 1 & ":" & i + 2).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 2)
        End If
        If Range("K" & i).Value = "Skimmer(4)" Then
            Rows(i + 1 & ":" & i + 3).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 3)
        End If
    Next i
End Sub
 
Upvote 0
Solution
VBA Code:
Sub Skimmer()
    lr = Range("K" & Rows.Count).End(xlUp).Row
   
    For i = lr To 1 Step -1
        If Range("K" & i).Value = "Skimmer(2)" Then
            Rows(i + 1 & ":" & i + 1).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 1)
        End If
        If Range("K" & i).Value = "Skimmer(3)" Then
            Rows(i + 1 & ":" & i + 2).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 2)
        End If
        If Range("K" & i).Value = "Skimmer(4)" Then
            Rows(i + 1 & ":" & i + 3).Insert
            Range(Cells(i, 1), Cells(i, Columns.Count)).Copy Range("A" & i + 1 & ":A" & i + 3)
        End If
    Next i
End Sub
this works great, thank you very much !
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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