Stopping an Autofilter macro if no value is found...

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
912
Office Version
  1. 365
Platform
  1. Windows
This code searches for a criteria value catval between the given dates d1 & d2:
Code:
    Range("A1:E1").Select
    Selection.AutoFilter
    With Sheets("REGISTER").Range("B1").CurrentRegion.Offset(1, 0)
    .AutoFilter Field:=2, Criteria1:=catval
    .AutoFilter Field:=3, Criteria1:=">=" & d1, Operator:=xlAnd, Criteria2:="<=" & d2
    .SpecialCells(xlVisible).Copy Sheets("REPORT").Range("A1")
    .AutoFilter
    End With
..works great as long as there is a value to be found. This requires populating the REGISTER sheet before it runs correctly. But what happens there is no value to be found like on day 1 of each month. The code bugs out. I would like it to generate a Msgbox that says "No value found" and simply stop running and return user control to the active sheet. Any ideas on how this is done and most importantly, at what point in the code the Msgbox line would be placed ?
Thanks for any help.
cr
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You could try using error handler like this.

Code:
Sub Filter_Stuff()

On Error GoTo myError:


    Range("A1:E1").Select
    Selection.AutoFilter
    With Sheets("REGISTER").Range("B1").CurrentRegion.Offset(1, 0)
    .AutoFilter Field:=2, Criteria1:=catval
    .AutoFilter Field:=3, Criteria1:=">=" & d1, Operator:=xlAnd, Criteria2:="<=" & d2
    .SpecialCells(xlVisible).Copy Sheets("REPORT").Range("A1")
    .AutoFilter
    End With

Exit Sub

myError:
MsgBox ("Something Went Wrong")
Err.Clear
Exit Sub


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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