Filter table and delete - code error

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
442
Office Version
  1. 365
Platform
  1. Windows
Guys,

I have found this code but i am getting error messages when running it.

lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete - this brings up and error as does ws.Activate

Thanks

Code:
[COLOR=#006CCC][FONT=Courier]Sub[/FONT][/COLOR][FONT=Courier] Delete_Rows_Based_On_Value_Table()[/FONT][FONT=Courier][COLOR=#007F00]'Apply a filter to a Table and delete visible rows[/COLOR]
[COLOR=#007F00]'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/[/COLOR]

[COLOR=#006CCC]Dim[/COLOR] lo [COLOR=#006CCC]As[/COLOR] ListObject

  [COLOR=#007F00]'Set reference to the sheet and Table.[/COLOR]
  [COLOR=#006CCC]Set[/COLOR] lo = Sheet3.ListObjects(1)
  ws.Activate
  
  [COLOR=#007F00]'Clear any existing filters[/COLOR]
  lo.AutoFilter.ShowAllData
  
  [COLOR=#007F00]'1. Apply Filter[/COLOR]
  lo.Range.AutoFilter Field:=4, Criteria1:="Product 2"
  
  [COLOR=#007F00]'2. Delete Rows[/COLOR]
  Application.DisplayAlerts = [COLOR=#006CCC]False[/COLOR]
    lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = [COLOR=#006CCC]True[/COLOR]

  [COLOR=#007F00]'3. Clear Filter[/COLOR]
  lo.AutoFilter.ShowAllData

[/FONT][COLOR=#006CCC][FONT=Courier]End[/FONT][/COLOR][COLOR=#006CCC][FONT=Courier]Sub[/FONT][/COLOR]
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try like this instead

Code:
Sub Delete_Rows_Based_On_Value_Table() 'Apply a filter to a Table and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/

    Dim lo As ListObject, ws As Worksheet
    Set ws = ActiveSheet

'Set reference to the sheet and Table.
    Set lo = ws.ListObjects(1)
      
'Clear any existing filters
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
    
'1. Apply Filter
    lo.Range.AutoFilter Field:=4, Criteria1:="Product 2"
      
'2. Delete Rows
    Application.DisplayAlerts = False
        lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
    
'3. Clear Filter
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
End Sub
 
Last edited:
Upvote 0
Thanks, works great until i add an array for the criteria as per below: Runtime error 1004 no cells were found? Can you help?

Many thanks

Code:
Sub Delete_Rows_Based_On_Value_Table() 'Apply a filter to a Table and delete visible rows'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/


    Dim lo As ListObject, ws As Worksheet
    Set ws = ActiveSheet


'Set reference to the sheet and Table.
    Set lo = ws.ListObjects(1)
      
'Clear any existing filters
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
    
'1. Apply Filter
    lo.Range.AutoFilter Field:=4, Criteria1:=Array("1st Attempt made", "2nd Attempt made", "3rd Attempt Made")
      
'2. Delete Rows
    Application.DisplayAlerts = False
        lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
    
'3. Clear Filter
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
End Sub
 
Upvote 0
Code:
Sub Delete_Rows_Based_On_Value_Table() 'Apply a filter to a Table and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/
'now amended in several places

    Dim lo As ListObject, ws As Worksheet, [COLOR=#ff0000]arr[/COLOR]
    Set ws = ActiveSheet
    
'Set reference to the sheet and Table.
    Set lo = ws.ListObjects(1)
    [COLOR=#ff0000]arr = Array("1st Attempt made", "2nd Attempt Made", "3rd Attempt Made")[/COLOR]
      
'Clear any existing filters
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
    
'1. Apply Filter
    lo.Range.AutoFilter Field:=4, Criteria1:=[COLOR=#ff0000]arr[/COLOR][COLOR=#800080], Operator:=xlFilterValues[/COLOR]
      
'2. Delete Rows
    Application.DisplayAlerts = False
        lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
    
'3. Clear Filter
    On Error Resume Next
    lo.AutoFilter.ShowAllData
    On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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