Delete filtered/visible rows except headers in a table

aghaffar82

Board Regular
Joined
Jun 13, 2019
Messages
65
Office Version
  1. 365
Platform
  1. Windows
Hello Sirs,

I am filtering data from multiple columns with multiple criteria (manually), but want to delete the filtered out (visible) rows, except headers, with VBA.

The reason being I want to do this because my data table has around 1 million rows and once I am done with filtering there are still 400k-500k rows which take a lot of time deleting manually.

Thank you so much in advance for your kind help.
Best Regards
 
Run this after you've applied the filter

You might need to change the Worksheet name and range to suit your setup
VBA Code:
Sub DeleteFilteredRows()
   
    Dim last_row As Long
   
    Application.ScreenUpdating = False
   
    With Sheets("Sheet1")
        last_row = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
        .Range("A2", "A" & last_row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
   
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Run this after you've applied the filter

You might need to change the Worksheet name and range to suit your setup
VBA Code:
Sub DeleteFilteredRows()
  
    Dim last_row As Long
  
    Application.ScreenUpdating = False
  
    With Sheets("Sheet1")
        last_row = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  
        .Range("A2", "A" & last_row).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
  
    Application.ScreenUpdating = True

End Sub


Thank you

I will update you Sir, Once I have it a try. Regards
 
Upvote 0

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