VBA Code to delete rows filtered by a slicer - without a loop

pliskers

Active Member
Joined
Sep 26, 2002
Messages
461
Office Version
  1. 2016
Platform
  1. Windows
I have a table with tens of thousands of rows, filtered via slicers. Once slicer settings are applied, I'd like all of the visible cells to be deleted, and then clear the filter so that the surviving rows are visible. I've tried code that uses a loop-through, but it takes far too long to cycle through each row individually.

Keeping in mind that this is a table (named "Worksheet") and that the rows are filtered via slicers, is there a more efficient way to clear the visible rows and then display the ones that remain?

Thank you in advance!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
The following code first checks if the table is filtered. If so, it deletes the filtered rows, and then clears the filter. Change the name of the slicer accordingly.

VBA Code:
    With Worksheets("Sheet1").ListObjects("Worksheet")
        If .AutoFilter.FilterMode Then
            .DataBodyRange.EntireRow.Delete
            .Slicers("SlicerName").SlicerCache.ClearAllFilters 'change the slicer name accordingly
        End If
    End With

Hope this helps!
 
Upvote 0
The following code first checks if the table is filtered. If so, it deletes the filtered rows, and then clears the filter. Change the name of the slicer accordingly.

VBA Code:
    With Worksheets("Sheet1").ListObjects("Worksheet")
        If .AutoFilter.FilterMode Then
            .DataBodyRange.EntireRow.Delete
            .Slicers("SlicerName").SlicerCache.ClearAllFilters 'change the slicer name accordingly
        End If
    End With

Hope this helps!
Thanks! I found that I was getting an error message because there were some grouped columns hidden - that was generating the error. The code works great, thanks!
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

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