VBA delete rows based on cell criteria

kwg

Board Regular
Joined
Apr 26, 2003
Messages
75
I am trying to create code for a table to filter on all in the filter box list but one item, delete rows that are visible, across multiple rows in the workbook, (my sheets in excel contain over 5000 rows). the code below works but I need it to delete rows NOT equal to Product25. I am using a table with over 6000 lines and over 50 items to select from on the filter column


Option Explicit

Sub Delete_Rows_Based_On_Value_Table()
'Apply a filter to a Table and delete visible rows

Dim lo As ListObject

'Set reference to the sheet and Table.
Set lo = Sheet3.ListObjects(1)
lo.Parent.Activate 'Activate sheet that Table is on.

'Clear any existing filters
lo.AutoFilter.ShowAllData

'1. Apply Filter
lo.Range.AutoFilter Field:=4, Criteria1:="Product25"


'2. Delete Rows
Application.DisplayAlerts = False
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True

'3. Clear Filter
lo.AutoFilter.ShowAllData

End Sub
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I tried it on a listobject I created and it worked. Generally speaking if If this works (for your orginal code)

Code:
lo.Range.AutoFilter Field:=4, Criteria1:="Product25"

then this should also work

Code:
 lo.Range.AutoFilter Field:=4, Criteria1:="<>Product25"

The 424 runtime error suggests something is amiss for your 'lo' object declaration. Try using the debugger to single step though your original unmodified code as posted in #1 and see if you can execute it without errors. I you can then change the criteria to "<>Product25"

Code:
 lo.Range.AutoFilter Field:=4, Criteria1:="<>Product25"

And repeat the exercise. If you get an error, on which line does it happen?
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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