How to avoid an error message when the filter has already been applied

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,
In the below code I filter in my case "warehouse 1", but when for some reason this already has been done I get an error message. Is there a way to avoid this, beside not pushing the button. So somekind of warning or message instead of an error.

Thank you for your time and have a great day.


Romano

VBA Code:
Private Sub FilterMag1_Click()
 Range("Locatie[[#Headers],[Magazijn]]").Select
    ActiveSheet.ListObjects("Locatie").Range.AutoFilter Field:=8, Criteria1:= _
        "1"
        
        Dim r As Range, q As Range, a As Range, b As Range
Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects("Locatie")

Set q = tbl.DataBodyRange.Columns(1)
Set r = q.SpecialCells(xlCellTypeVisible)

tbl.AutoFilter.ShowAllData

r.EntireRow.Hidden = True
q.SpecialCells(xlCellTypeVisible).Rows.ClearContents
q.EntireRow.Hidden = False

tbl.Range.Sort Key1:=tbl.Range.Cells(1), Order1:=xlAscending, Header:=xlYes

'first blank row
Set a = tbl.Range.Cells(1).End(xlDown).Offset(1)
'last row
Set b = a.End(xlDown)

If Not Intersect(b, tbl.DataBodyRange) Is Nothing Then
    Range(a, b).Rows.Delete xlUp
Else 'if there's only 1 blank row
    a.Rows.Delete xlUp
End If

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I understand that you want to remove everything different from "1", so try the following:

VBA Code:
Private Sub FilterMag1_Click()
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  With ActiveSheet.ListObjects("Locatie")
    .AutoFilter.ShowAllData
    .Range.AutoFilter Field:=8, Criteria1:="<>1"
    .AutoFilter.Range.Offset(1).Delete
    .AutoFilter.ShowAllData
  End With
  
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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