Erase row if column contains certain value

Hidden Dan

Board Regular
Joined
Dec 7, 2016
Messages
63
Hello,

I got this very simple macro that allows user to erase a complete row. It is manually activated by placing cursor in a certain row/cell and executed by CTRL+SHFT+E.

Code:
Sub Erase_item()
    ActiveCell.EntireRow.Select
    Selection.Delete Shift:=xlUp
    Range("A6").Select
End Sub

After deleting row active cell will be A6. This works fine for me. But I would like to incorporate a safety measure. Only that row may be deleted if it contains a certain value.

Suppose my data is placed in A10 : D20. Column A may contain 0 or 1. You may only delete row 12 if value A12 is 0. If A12 would be 1, deletion is prohibited. Similar any row between 10 and 20.

So in practice next will be happen:
- Cursor is placed in cell B12
- Value of A12 is read
- Macro is executed
- if A12 = 0, row is deleted
- if A12 = 1, row is not deleted
- Cursor gets back to A6


How can I add this functionality to my existing script ?

Thanks, Dan
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Code:
Sub Erase_item()
    With Rows(ActiveCell.Row)
      If .Range("A1").Value = 0 Then .Delete
    End With
    Range("A6").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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