Loop All Tables, Filter and Delete Visible Rows

*shudder*

Well-known Member
Joined
Aug 20, 2009
Messages
510
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I would like to be able to loop though all tables in a workbook, apply a filter and then delete the visible cells.

I have the following code which filters all of the tables:

VBA Code:
Sub Filter_Data()

Dim sht As Worksheet
Dim lstobj As ListObject
Dim Port_Name As String

Port_Name = ThisWorkbook.Sheets("Home").Range("A1").Value

For Each sht In ThisWorkbook.Worksheets
    For Each lstobj In sht.ListObjects
        With lstobj
            .AutoFilter.ShowAllData
            .DataBodyRange.AutoFilter Field:=1, Criteria1:="<>" & Port_Name
            .DataBodyRange.SpecialCells(xlCellTypeVisible).EntireRow.delete
            .AutoFilter.ShowAllData
        End With
    Next lstobj
Next sht

End Sub

However the delete visible cells element only works on the first table and then throws up a run-time 1004 error - Delete method of Range class failed for all of the other tables.

Many thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:
VBA Code:
Sub Filter_Data()

    Dim sht As Worksheet
    Dim lstobj As ListObject
    Dim Port_Name As String

    Port_Name = ThisWorkbook.Sheets("Home").Range("A1").Value
   
    For Each sht In ThisWorkbook.Worksheets
        For Each lstobj In sht.ListObjects
            With lstobj
                .AutoFilter.ShowAllData
                .DataBodyRange.AutoFilter Field:=1, Criteria1:="<>" & Port_Name
            End With
            Delete_Visible_Table_Rows lstobj
        Next lstobj
    Next sht

End Sub


Private Sub Delete_Visible_Table_Rows(table As ListObject)

    Dim visibleAreas As Range, visibleArea As Range
    Dim deleteRows As Range

    With table

        'Get visible rows
       
        Set visibleAreas = .DataBodyRange.SpecialCells(xlCellTypeVisible)
       
        'Unfilter the table to show all rows
               
        .Range.AutoFilter

        'Find all the rows that were visible
               
        Set deleteRows = Nothing
        For Each visibleArea In visibleAreas.Areas
            If deleteRows Is Nothing Then
                Set deleteRows = visibleArea
            Else
                Set deleteRows = Union(deleteRows, visibleArea)
            End If
        Next
       
        'Delete all the rows that were visible
       
        If Not deleteRows Is Nothing Then
            deleteRows.Delete
        End If
       
    End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,383
Messages
6,136,249
Members
450,001
Latest member
KWeekley08

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