VBA filter and delete

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
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
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,934
Office Version
  1. 365
Platform
  1. Windows
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
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,934
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105

ADVERTISEMENT

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"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,934
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
 .Range("A1:G1").AutoFilter 7, "<>" & .Range("K3").Value
where K3 has 302
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,934
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,511
Messages
5,596,581
Members
414,079
Latest member
Frills

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
Top