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

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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,215,129
Messages
6,123,218
Members
449,091
Latest member
jeremy_bp001

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