Macro to find row and delete it

drrafa

New Member
Joined
Nov 17, 2013
Messages
2
Hello

I need a macro to run every time I press a button (form control).
It should read the cell value (number) in cell i3
then it should look in columm B3-B20 for that value
in case it finds it, it should delete that row (row a-g)
it should also create a new row so the total number of rows remains the same
in case it finds no match, it should do nothing.

Thx
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Welcome to MrExcel board...

be sure to make a copy of your file before trying the macro.

Code:
Sub Delete_with_Value()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Application.DisplayAlerts = False
    
    x = Range("I3")
    Cells.AutoFilter Field:=2, Criteria1:=x
    Range("B2:B" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("B2:B" & LR).Delete
    Cells.AutoFilter
MsgBox "Rows with value of " & x & " have been deleted"
Application.DisplayAlerts = True
    
End Sub
 
Upvote 0
That was real fast.
I got a run time erro 1004 - autofilter method of range class failed.



Welcome to MrExcel board...

be sure to make a copy of your file before trying the macro.

Code:
Sub Delete_with_Value()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Application.DisplayAlerts = False
    
    x = Range("I3")
    Cells.AutoFilter Field:=2, Criteria1:=x
    Range("B2:B" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("B2:B" & LR).Delete
    Cells.AutoFilter
MsgBox "Rows with value of " & x & " have been deleted"
Application.DisplayAlerts = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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