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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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