Macro to Delete any Row in entire sheet based on Cell Values

adibakale

Board Regular
Joined
Apr 10, 2015
Messages
52
I need to erase the entire row based on 2 different cell values. I would like the Macro to all rows and columns on the sheet and delete the entire row if it finds a cell containing these 2 values:

1. WORK OF
2. RUN DATE

Thank you - your help is greatly appreciated
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I need to erase the entire row based on 2 different cell values. I would like the Macro to all rows and columns on the sheet and delete the entire row if it finds a cell containing these 2 values:

1. WORK OF
2. RUN DATE
Please clarify what you mean by "a cell containing"... do you mean both of the lines (with the number/dot/space in front) are in the same cell? If so, are they on two separate lines (as shown) within that same single cell and with the numbers in front always be 1. and 2.?
 
Upvote 0
Cells containing only the words WORK OF and RUN DATE. Both of these cells are in 2 different rows. They also contain a Date and other words. Here is exactly what each cell in each row looks like:

WORK OF 05/05/15 PAGE
RUN DATE 05/06/15 TIME


<colgroup><col></colgroup><tbody>
</tbody>
 
Upvote 0
Cells containing only the words WORK OF and RUN DATE. Both of these cells are in 2 different rows. They also contain a Date and other words. Here is exactly what each cell in each row looks like:

WORK OF 05/05/15 PAGE
RUN DATE 05/06/15 TIME


<colgroup><col></colgroup><tbody>
</tbody>
I is not clear if these words are contained within a single column or not. If they can be located anywhere, then use this macro...

Code:
Sub DeleteRowsContainingWORKOFandRUNDATE()
  With Cells
    .Replace "WORK OF*", "#N/A", xlWhole
    .Replace "RUN DATE*", "#N/A", xlWhole
    'On Error Resume Next
    Intersect(Cells, .SpecialCells(xlConstants, xlErrors).EntireRow).Delete
    On Error GoTo 0
  End With
End Sub

If they are restricted to a single column, then use this instead (replace the A with the correct column letter designation)...

Code:
Sub DeleteRowsContainingWORKOFandRUNDATE()
  With Columns("[B][COLOR="#FF0000"]A[/COLOR][/B]")
    .Replace "WORK OF*", "#N/A", xlWhole
    .Replace "RUN DATE*", "#N/A", xlWhole
    'On Error Resume Next
    Intersect(Cells, .SpecialCells(xlConstants, xlErrors).EntireRow).Delete
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
I am getting a

Run-time error
'1004':
No cells were found.

Debug: Intersect(Cells, .SpecialCells(xlConstants, xlErrors).EntireRow).Delete


 
Upvote 0
I am getting a

Run-time error
'1004':
No cells were found.

Debug: Intersect(Cells, .SpecialCells(xlConstants, xlErrors).EntireRow).Delete

That means you don't have any cells with the text you wanted to find. The error message was supposed to be suppressed by the line above which I commented out while debugging and forgot to reactivate it... remove the apostrophe from the line above the one you posted and the error message will go away.
 
Upvote 0
That worked, thank you. There are 7 spaces before the WORK OF and RUN DATE. Adding the 7 spaces in the macro worked, but is there a way to just ignore these spaces?
 
Upvote 0
That worked, thank you. There are 7 spaces before the WORK OF and RUN DATE. Adding the 7 spaces in the macro worked, but is there a way to just ignore these spaces?
In Message #3 you said "Here is exactly what each cell in each row looks like" and what you posted did not show any leading characters at all, so I did not try to account for any. The solution... put an asterisk before them in the Replace function calls...
Code:
Sub DeleteRowsContainingWORKOFandRUNDATE()
  With Cells
    .Replace "[B][COLOR="#FF0000"]*[/COLOR][/B]WORK OF*", "#N/A", xlWhole
    .Replace "[B][COLOR="#FF0000"][/COLOR][COLOR="#FF0000"]*[/COLOR][/B]RUN DATE*", "#N/A", xlWhole
    On Error Resume Next
    Intersect(Cells, .SpecialCells(xlConstants, xlErrors).EntireRow).Delete
    On Error GoTo 0
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,204
Messages
6,123,630
Members
449,109
Latest member
Sebas8956

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