Delete rows based on range of values found in Column A

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi Guys,
I'm SO new to this, I don't even really understand how to put macros into the Excel coding section. :confused:
I want to delete all the rows that show anything BUT a value between 300 and 400 in column A.
I was trying to modify some of the other 'delete row' codes that have been supplied, but no luck. :(
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello and welcome to the board,

See if this works for you.

Enter this code in a standard module

Take a look here about where to paste code

Code:
Sub DelOliviar()
    Dim LR As Long, i As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = LR To 2 Step -1
        If Range("A" & i).Value >= 300 And Range("A" & i).Value >= 400 Then Rows(i).Delete
    Next i
End Sub
 
Upvote 0
This one might be even faster depending on how big your dataset is.

Code:
Sub FilterDeleteOliviar()
    With Range("A1", Range("A" & Rows.Count).End(xlUp))
        .AutoFilter Field:=1, Criteria1:=">=300", Operator:=xlAnd, Criteria2:="<=400"
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub

The difference is the first code loops through all cells in Column A and the latter filter and deletes in one swipe.
 
Upvote 0
Yes this first one is taking a really long time. I will see what happens with the second one once this first version finishes its thing. Thank you so much though.
 
Upvote 0
Sorry Jeff, Its just not working. Nothing has been deleted. I have tried both.
Row 1 has headings in it. Could that be the problem?
 
Upvote 0
Heading in row 1 are not a problem. Do you want to keep everything between 300 and 400 or delete between 300 and 400?

Before Macro
Excel Workbook
A
1Data
2223
3375
4444
5325
Sheet2


After Macro
Excel Workbook
A
1Data
2223
3444
Sheet2


Macro
Code:
Sub FilterDeleteOliviar()
    With Range("A1", Range("A" & Rows.Count).End(xlUp))
        .AutoFilter Field:=1, _
                    Criteria1:=">=300", _
                    Operator:=xlAnd, _
                    Criteria2:="<=400"
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0
Keep everything between 300 and 400.
Well technically everything between 300 and 399. Also including "300".
 
Upvote 0
Keep everything between 300 and 400.
Well technically everything between 300 and 399. Also including "300".

Change to:
Code:
Sub FilterDeleteOliviar()
    With Range("A1", Range("A" & Rows.Count).End(xlUp))
        .AutoFilter Field:=1, _
                    Criteria1:="<300", _
                    Operator:=xlOr, _
                    Criteria2:=">399"
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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