Inserting a Blank Row After A Row With A Certain Value

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,538
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What is the best way to find the last row in which the value in column R begins with a "D", and then insert a row below it?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This Code Does The Job :
Code:
Sub InsertRow()
i = 0
For Each cell In Range("r" & Cells(Rows.Count, 18).End(xlUp).Row & ":R1")
    If Left(cell.Value, 1) = "D" Then
       i = i + 1
       If i = Application.CountIf(Range("R:R"), "D*") Then
          cell.Offset(1, 0).EntireRow.Select
          Selection.Insert xlUp
          Exit For
       Else
          GoTo 1
       End If
    Else
       GoTo 1
    End If
1 Next
End Sub

ZAX
 
Upvote 0
Hi Jenn,

Try this:

Code:
Option Explicit
Sub Macro1()

    'Written by Trebor76
    'Visit my website www.excelguru.net.au
    
    Dim lngEndRow As Long, _
        lngMyRow As Long
    Dim blnRowInserted As Boolean
    
    lngEndRow = Cells(Rows.Count, "R").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    For lngMyRow = lngEndRow To 2 Step -1
        If Left(Range("R" & lngMyRow - 1), 1) = "D" Then
            Rows(lngMyRow).EntireRow.Insert
            blnRowInserted = True
            Exit For
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True
    
    If blnRowInserted = True Then
        MsgBox "Row has been inserted"
    Else
        MsgBox "Row could not be inserted as the desired criteria was not met"
    End If

End Sub

Regards,

Robert
 
Upvote 0
Wow. Great answers folks. Each worked wonderfully.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,203,206
Messages
6,054,146
Members
444,703
Latest member
pinkyar23

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