How to Filter and Delete Rows with VBA in Excel – 2 Methods

The sample dataset showcases Salesman, Product, and Net Sales of a company.

Filter and Delete Rows with VBA in Excel


Method 1 – Filtering the Visible Rows and Deleting them with VBA in Excel

In the dataset below, product AC is the single criterion. Only the rows with AC will be visible.

Filter the Visible Rows and Delete with VBA in Excel


1.1 Single Criterion

STEPS:

  • Go the Developer tab and select Visual Basic.

Filter the Visible Rows and Delete with VBA in Excel

  • In the new window, in Insert, select Module.

Filter the Visible Rows and Delete with VBA in Excel

  • Another window will be displayed.
  • Enter the code in the module window.
Sub FilterDeleteVisible()
Dim range As range
Set range = Selection
range.AutoFilter Field:=2, Criteria1:="AC"
range.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.AutoFilterMode = False
End Sub

Filter the Visible Rows and Delete with VBA in Excel

  • Close Visual Basic.
  • Select the range including the header.

Filter the Visible Rows and Delete with VBA in Excel

  • In the Developer tab, select Macros.

  • In Macro name, select FilterDeleteVisible .

  • Click Run and you’ll only see the rows with the product Cable.


1.2 Multiple Criteria

Filter the Visible Rows and Delete with VBA in Excel

STEPS:

  • Click Visual Basic in the Developer tab.

Filter the Visible Rows and Delete with VBA in Excel

  • In the new window, choose Insert and select Module.

Filter the Visible Rows and Delete with VBA in Excel

  • Enter the following code in the module window.
Sub MultipleCriteria()
Dim rows As range
Dim range As range
Set range = Selection
range.AutoFilter Field:=2, Criteria1:="AC"
range.AutoFilter Field:=3, Criteria1:=">10000"
For Each rows In range.rows
Next
range.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.AutoFilterMode = False
End Sub

Filter the Visible Rows and Delete with VBA in Excel

  • Close Visual Basic.
  • Select the range.

Filter the Visible Rows and Delete with VBA in Excel

  • In the Developer tab, select Macros.

  • In the dialog box, select  MultipleCriteria in  Macro name.

  • Click Run.

This is the output.

Read More: How to Use Macro to Delete Rows Based on Criteria in Excel


Method 2 -Using  Excel VBA to Delete Hidden Filtered Rows

In the dataset below, two criteria will be applied: product AC and  Net Sales Greater than $10,000. You want to delete the hidden rows which don’t meet the criteria.

Excel VBA to Delete Filtered Rows That are Hidden

STEPS:

  • Select Visual Basic in the Developer tab.

Excel VBA to Delete Filtered Rows That are Hidden

  • In the new window, choose Insert and select Module.

Excel VBA to Delete Filtered Rows That are Hidden

  • Enter the following code.
Sub FilterDeleteHidden()
Dim uni As range
Dim rows As range
Dim range As range
Set range = Selection
range.AutoFilter Field:=2, Criteria1:="AC"
range.AutoFilter Field:=3, Criteria1:=">10000"
For Each rows In range.rows
If rows.Hidden Then
If Not uni Is Nothing Then
Set uni = union(uni, rows)
Else
Set uni = rows
End If
End If
Next
uni.Delete
ActiveSheet.AutoFilterMode = False
End Sub

Excel VBA to Delete Filtered Rows That are Hidden

  • Close Visual Basic.
  • Select the range.

Excel VBA to Delete Filtered Rows That are Hidden

  • Select Macros in the Developer tab.

Excel VBA to Delete Filtered Rows That are Hidden

  • In the dialog box, select FilterDeleteHidden in Macro name.

  • Click Run.
  • Click OK.

Hidden rows will be deleted and only visible rows will be displayed.

Read More: Excel VBA to Delete Row If Cell Contains Partial Text


Download Practice Workbook

Download the following workbook.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Aung Shine
Aung Shine

Aung Shine completed his bachelor’s in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. It has been almost 2 years since he joined SOFTEKO and actively working on the ExcelDemy project. Currently he works as a Team Leader where he guides his team members to create technical content. He has published 150+ articles and reviewed 50+ articles. He has also solved various user problems before. He has interests in Data Analysis, Power Query, Advanced Excel,... Read Full Bio

1 Comment
  1. Thanks. I have a workbook in which many thousands of rows needed to be deleted at a time. This was very slow, tens of seconds, using this method. I had an idea and found it to be very fast. Instead of deleting rows, I clear contents and then resort the data thus removing all the blank rows created. It is very fast and where appropriate, perhaps a good alternative to have in the toolbox

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo