Delete row if cell contains a certain value AND the row above is empty

hip2b2

Board Regular
Joined
May 5, 2003
Messages
135
Office Version
  1. 2019
Platform
  1. Windows
I need to delete a row where the value in Col B (of that same row( contains specific text, AND the row above is blank.

Thanks

hip
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:
VBA Code:
Sub DeleteRowIf()
Dim i As Long, lr As Long
lr = Cells(Rows.Count, 2).End(xlUp).Row

For i = 2 To lr
If Range("B" & i).Value = "Specific" And Range("B" & i - 1).Value = "" Then
Cells(i, 2).EntireRow.Delete
End If
Next i
End Sub
 
Upvote 0
Thanks it works perfectly!!! except I want to run it from within a "dogs breakfast" of a maco and the code fails with " , lr As Long".

Any suggestion?
 
Upvote 0
I think you have two lr declare at your macro. Change lr to Lr1 at all of my code and test it. if again error, please upload total macro here to check.
 
Upvote 0
Is it possible to modify the code to make the contents less specific, so that the Cell in Col B contains "Specific" and any other text AND the row above is blank...

I tried the following (which of course did not work).

VBA Code:
Dim i As Long, lr1 As Long
    lr1 = Cells(Rows.Count, 2).End(xlUp).Row
    For i = 2 To lr
    If Range("B" & i).Value = "Specific" & strName & "*" And Range("B" & i - 1).Value = "" Then
    Cells(i, 2).EntireRow.Delete
    End If
    Next i
    End With
    h.AutoFilterMode = False

Thanks

hip
 
Upvote 0
Use Like Option.
Try This:

VBA Code:
Dim i As Long, lr1 As Long
    lr1 = Cells(Rows.Count, 2).End(xlUp).Row
    For i = 2 To lr
    If Range("B" & i).Value  Like "*" & "Specific" &  "*" And Range("B" & i - 1).Value = "" Then
    Cells(i, 2).EntireRow.Delete
    End If
    Next i
    End With
    h.AutoFilterMode = False
 
Upvote 0
Solution

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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