Delete Blank Row Before Particular Text

Mohanmoni

New Member
Joined
Mar 9, 2016
Messages
15
Hi,

I am looking for VBA Code that can delete blank row above particular text. For Example..

A1: John Kenneth
A2:
A3: Manager
A4: California

Like this i have 10,000 records, Now I want delete the row A2 which is above "Manager" keyword. Please suggest.

Thank You,
Mohan S
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here's one way to do it
Code:
Sub DeleteBlankText()
    Dim I As Long, Lastrow As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For I = Lastrow To 2 Step -1
        If Cells(I, "A") = "" And Cells(I + 1, "A") = "Manager" Then
            Rows(I).EntireRow.Delete
        End If
    Next I
End Sub

Thats in its basic form
 
Upvote 0
Hi Momentman,

Thanks for your reply...but this code works only particular word called "Manager". I want keyword consideration example.. this may consider "Executive Manager" or "Digital Manager"... But "Manager" is common keyword. Please suggest code for this.
 
Upvote 0
This code appears longer but should work much quicker on your fairly large data set. Test in a copy of your workbook.

Code:
Sub Del_Rws()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, lc As Long
  
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 2 To UBound(a) - 1
    If IsEmpty(a(i, 1)) Then
      If InStr(1, a(i + 1, 1), "manager", vbTextCompare) > 0 Then
        b(i, 1) = 1
        k = k + 1
      End If
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    lc = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
    With Range("A1").Resize(UBound(b), lc)
      .Columns(lc).Value = b
      .Sort Key1:=.Columns(lc), Order1:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Offset(1).Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = False
  End If
End Sub
 
Upvote 0
Hi Momentman,

Thanks for your reply...but this code works only particular word called "Manager". I want keyword consideration example.. this may consider "Executive Manager" or "Digital Manager"... But "Manager" is common keyword. Please suggest code for this.
Surely Peter's code will be faster. But to answer your question, this is what i would do
Code:
Sub DeleteBlankText()
    Dim I As Long, Lastrow As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For I = Lastrow To 2 Step -1
        If Cells(I, "A") = "" And InStr(1, Cells(I + 1, "A"), "Manager") > 0 Then
            Rows(I).EntireRow.Delete
        End If
    Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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