Deleting all options in a filter, except one, using VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've got a list 4 statuses in column U of a Sheet (Active, Inactive, Pending Active and Pending Inactive).

I thought this code would work, but it deletes ALL 4 options, instead of leaving the Active rows.

Does anyone know how to modify this so that it deletes everything BUT the Active rows, please?

Also, do I need to repeat the code for every option?

TIA

VBA Code:
    On Error Resume Next
    With ActiveSheet
        .Range("U1:U").AutoFilter 20, "Inactive"
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .Automodefilter = False
    End With
    On Error GoTo 0
    
        On Error Resume Next
    With ActiveSheet
        .Range("U1:U").AutoFilter 20, "Pending Inactive"
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .Automodefilter = False
    End With
    On Error GoTo 0
    
        On Error Resume Next
    With ActiveSheet
        .Range("U1:U").AutoFilter 20, "Pending Active"
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .Automodefilter = False
    End With
    On Error GoTo 0
    
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False

Status
Inactive
Active
Pending Inactive
Pending Active
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:

VBA Code:
Sub Deleting_Options()
  With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A1:U" & .Range("U" & Rows.Count).End(3).Row).AutoFilter 21, Array("Inactive", "Pending Active", "Pending Inactive"), xlFilterValues
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
  End With
End Sub
 
Upvote 0
Hi Dante

Thanks for the prompt response.

I tried that, but then got a "Run time error: 1004. Autofilter method of range of class failed."

Do you know why that may have happened?

And thanks for the other solution that you posted recently, on a different question - it was helpful!!
 
Upvote 0
Also, the number of rows of data will vary....

I don't know if that will make a difference to how the code needs to be written?
 
Upvote 0
I tried that, but then got a "Run time error: 1004. Autofilter method of range of class failed."
Do you know why that may have happened?

It works for me, maybe the excel version.

Here another approach
VBA Code:
Sub Delete_Option_2()
  Dim lr As Long, i As Long, a, r As Range
  Application.ScreenUpdating = False
  
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  lr = Range("U" & Rows.Count).End(3).Row
  Set r = Range("A" & lr + 1)
  a = Range("U2:U" & lr)
  For i = 1 To UBound(a)
    Select Case a(i, 1)
      Case "Inactive", "Pending Active", "Pending Inactive"
        Set r = Union(r, Range("A" & i + 1))
    End Select
  Next i
  r.EntireRow.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your help! It was much appreciated!
 
Upvote 0
Try this:

VBA Code:
Sub Deleting_Options()
  With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A1:U" & .Range("U" & Rows.Count).[S][COLOR=rgb(209, 72, 65)]End(3)[/COLOR][/S].Row).AutoFilter 21, Array("Inactive", "Pending Active", "Pending Inactive"), xlFilterValues
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    .ShowAllData
  End With
End Sub
For anyone searching like mad for a fix for this. This works with one small change!
Remove the End(3).
 
Upvote 0

Forum statistics

Threads
1,223,934
Messages
6,175,485
Members
452,647
Latest member
MatthewBiersay

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