Forum Discussion
HELP PLEASE with Macro: delete row if value in row D is below "x" or above "y"
- Mar 28, 2018
The most efficient way I know to delete rows using VBA is to use auto-filter feature
You can place this code into a module and try to change the sheet names range and variables x , y to the one you have.
I wrote this code for the example I have attached.
you can see that it runs the code on Sheet1 and the range is set after the 25th row, it will ignore the previous rows up to 25th row. ws.Range("A25:O" & lastRow) also you can change your column from O to whatever your column is
you can assign values to x and y in the code.
if you download the workbook and you run the code in it. you will see what it will do to the data.
Please remember when running any macro, run it on a copy of workbook meaning (you must have a backup copy) because you cannot undo a macro action, just in case if you click on save by accident.
Sub Test() Dim x As Double, y As Double x = 7 y = 4 Dim ws As Worksheet Dim rng As Range Dim lastRow As Long Set ws = ActiveWorkbook.Sheets("Sheet1") lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row Set rng = ws.Range("A25:O" & lastRow) With rng .AutoFilter Field:=4, Criteria1:="<" & x, Operator:=xlAnd, Criteria2:=">" & y .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With ws.AutoFilterMode = False End Sub
The most efficient way I know to delete rows using VBA is to use auto-filter feature
You can place this code into a module and try to change the sheet names range and variables x , y to the one you have.
I wrote this code for the example I have attached.
you can see that it runs the code on Sheet1 and the range is set after the 25th row, it will ignore the previous rows up to 25th row. ws.Range("A25:O" & lastRow) also you can change your column from O to whatever your column is
you can assign values to x and y in the code.
if you download the workbook and you run the code in it. you will see what it will do to the data.
Please remember when running any macro, run it on a copy of workbook meaning (you must have a backup copy) because you cannot undo a macro action, just in case if you click on save by accident.
Sub Test()
Dim x As Double, y As Double
x = 7
y = 4
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws = ActiveWorkbook.Sheets("Sheet1")
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A25:O" & lastRow)
With rng
.AutoFilter Field:=4, Criteria1:="<" & x, Operator:=xlAnd, Criteria2:=">" & y
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ws.AutoFilterMode = False
End Sub
Thanks a lot Jamil, I will try running it on Tuesday when I am back in the office. Really appreciate your help, happy Easter!
- JamilMar 29, 2018Bronze ContributorYou are welcome. I am sure the macro will work for you. post back if you have any issue with it.
Happy Holiday!- paul.a.t.kruegerApr 04, 2018Copper Contributor
--> .AutoFilter Field:=4, Criteria1:="<" & x, Operator:=xlAnd, Criteria2:=">" & y
Pops out runtime error 1004: Autofilter method of range object couldnt be executed.
- JamilApr 04, 2018Bronze Contributorperhaps your data could have different number of columns. can you please post a screenshot of your sample data? Field 4 is the forth column, so make sure that the range you are running this code on matches, otherwise you could change the Field 4 to whatever column which your actual data is.