Insert 2 blank rows after row with a specific text

mictlantecuhtli

New Member
Joined
Oct 25, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi guys I need to insert two rows after a row with some content like this:

2021-10-28-01.png


To this:

2021-10-28-02.png


I haved found this code:

VBA Code:
Sub InsertRows()
        
        Dim Rw As Long
        Dim Rng As Range
        
        Set Rng = Sheets(1).Range("A4")
        Rw = Rng.Row
        
        Do
        
            If Sheets(1).Cells(Rw + 1, 1) <> Sheets(1).Cells(Rw, 1) Then
               Sheets(1).Cells(Rw + 1, 1).EntireRow.Insert
               Rw = Rw + 2
            Else
               Rw = Rw + 1
            End If
        
        Loop While Not Sheets(1).Cells(Rw, 1) = vbNullString

End Sub

The code works great inserting only one row, but need to insert two rows.

Thank's in advanced

Grettings from México
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Your example has 1 row inserted but your statement says 2 rows. Which is it?
Do the same names cluster together always?
 
Upvote 0
How about
VBA Code:
Sub InsertRows()
        
        Dim Rw As Long
        Dim Rng As Range
        
        Set Rng = Sheets(1).Range("A4")
        Rw = Rng.Row
        
        Do
        
            If Sheets(1).Cells(Rw + 1, 1) <> Sheets(1).Cells(Rw, 1) Then
               Sheets(1).Cells(Rw + 1, 1).Resize(2).EntireRow.Insert
               Rw = Rw + 3
            Else
               Rw = Rw + 1
            End If
        
        Loop While Not Sheets(1).Cells(Rw, 1) = vbNullString

End Sub
 
Upvote 0
Solution
This might do it:

Code:
Sub InsertRows()
'insert TWO rows above name
Dim lr As Long, i As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = lr To 4 Step -1
If Cells(i, 1) <> Cells(i - 1, 1) Then
 Cells(i, 1).Resize(2).EntireRow.Insert
Else
End If
Next i
End Sub
 
Upvote 0
How about
VBA Code:
Sub InsertRows()
       
        Dim Rw As Long
        Dim Rng As Range
       
        Set Rng = Sheets(1).Range("A4")
        Rw = Rng.Row
       
        Do
       
            If Sheets(1).Cells(Rw + 1, 1) <> Sheets(1).Cells(Rw, 1) Then
               Sheets(1).Cells(Rw + 1, 1).Resize(2).EntireRow.Insert
               Rw = Rw + 3
            Else
               Rw = Rw + 1
            End If
       
        Loop While Not Sheets(1).Cells(Rw, 1) = vbNullString

End Sub

Dude thank's it works like a charm.
 
Upvote 0
Hi guys, greetings

This works great but I had a new request, the same but insert rows into a selection of cells and not into a specified range of cells

Thank´s in advanced
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,033
Members
448,940
Latest member
mdusw

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