VBA filter and delete

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi

I'm trying to delete rows which DO NOT contain value "302" in colE to leave me with just the rows which do contain the value. Tried a loop but with over 65K rows it is taking too long.

Got the below but it is deleting all rows including those with 302 in colE. Anyone able to suggest where I'm going wrong?

VBA Code:
Sub FilterSubInv()





Application.ScreenUpdating = False



Columns(5).AutoFilter 1, Criteria1:="<>302"

With Range("a2", Range("g" & Rows.Count).End(3))

With ActiveSheet.AutoFilter.Range.Offset(1)

.EntireRow.Delete

End With

End With

Columns(7).AutoFilter



Application.ScreenUpdating = True



End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
VBA Code:
Sub andysh()
   With ActiveSheet
      .Range("A1:G1").AutoFilter 7, "<>302"
      .AutoFilter.rnage.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Thanks, Fluff

Had to change it to "<>*302*" but works a treat now. Must be something in the cells in the import file making it not like the 302 bit.
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0
Quick question (is there ever one of those?)

If I wanted to change this line so that the criteria is whatever is in cell K3 instead of "<>302", what would I need to change?

.Range("A1:G1").AutoFilter 7, "<>302"
 
Upvote 0
How about
VBA Code:
 .Range("A1:G1").AutoFilter 7, "<>" & .Range("K3").Value
where K3 has 302
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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