Msg box for nil return

Godders199

Active Member
Joined
Mar 2, 2017
Messages
313
Office Version
  1. 2013
Hello, I have written this maybe basic code, to filter a sheet by criteria selected in 3 cells, this works fine, however rather than go to a blank sheet with just the head rows when there are no cases, i would prefer a msg box to just say, "no matches , change criteria and search again" I have copied various codes i have found but not got any to work. Do i need to change my existing code? or is there something i can just add on the end.


With Sheets("cases available ").Range("a:ab")
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
.AutoFilter Field:=8, Criteria1:=Sheets("instructions ").Range("c23").Value, Operator:=xlFilterValues
.AutoFilter Field:=11, Criteria1:=Sheets("instructions ").Range("c35").Value, Operator:=xlFilterValues
.AutoFilter Field:=27, Criteria1:="=*" & Sheets("instructions ").Range("c26") & "*", Operator:=xlFilterValues
End With
Sheets("cases available ").Select


Any help as always appreciated.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
What row is the autofilter on?
 
Upvote 0
Untested, but try
Code:
With Sheets("cases available ").Range("a:ab")
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
.AutoFilter Field:=8, Criteria1:=Sheets("instructions ").Range("c23").Value, Operator:=xlFilterValues
.AutoFilter Field:=11, Criteria1:=Sheets("instructions ").Range("c35").Value, Operator:=xlFilterValues
.AutoFilter Field:=27, Criteria1:="=*" & Sheets("instructions ").Range("c26") & "*", Operator:=xlFilterValues
End With
[COLOR=#0000ff]If Range("A1").SpecialCells(xlVisible).Count = 1 Then
   MsgBox "No matches"
   Exit Sub
End If[/COLOR]
Sheets("cases available ").Select
 
Upvote 0
Thanks, I changed "a1" to E1 as column A does contain some blanks, column E will always have data. however Message box always shows even if the return is not zero . Is this because my marco Button is on the instructions tab, therefore it is looking at E1 on this tab, or is it looking at the cases available tab?

With Sheets("cases available ").Range("a:ab")
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
.AutoFilter Field:=8, Criteria1:=Sheets("instructions ").Range("c23").Value, Operator:=xlFilterValues
.AutoFilter Field:=11, Criteria1:=Sheets("instructions ").Range("c35").Value, Operator:=xlFilterValues
.AutoFilter Field:=27, Criteria1:="=*" & Sheets("instructions ").Range("c26") & "*", Operator:=xlFilterValues
End With

If Range("e1").SpecialCells(xlVisible).Count = 1 Then
MsgBox "No matches"
Exit Sub
End If
Sheets("cases available ").Select
End Sub
 
Upvote 0
Is this because my marco Button is on the instructions tab, therefore it is looking at E1 on this tab
Yes that's correct. Try this instead
Code:
With Sheets("cases available ").Range("a:ab")
   If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
   .AutoFilter Field:=8, Criteria1:=Sheets("instructions ").Range("c23").Value, Operator:=xlFilterValues
   .AutoFilter Field:=11, Criteria1:=Sheets("instructions ").Range("c35").Value, Operator:=xlFilterValues
   .AutoFilter Field:=27, Criteria1:="=*" & Sheets("instructions ").Range("c26") & "*", Operator:=xlFilterValues
End With
If Sheets("cases available ").Range("E1").SpecialCells(xlVisible).Count = 1 Then
   MsgBox "No matches"
   Exit Sub
End If
Sheets("cases available ").Select
 
Upvote 0
HI, this returns this error. RUntime errro 6, Overflow
when i debug the following line is highlighted

If Sheets("cases available ").Range("a1").SpecialCells(xlVisible).Count = 1 Then

the autofilter has returned 5 results
 
Upvote 0
OK, how about
Code:
Dim Usdrws As Long
Usdrws = Sheets("cases available ").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

With Sheets("cases available ").Range("a:ab")
   If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
   .AutoFilter Field:=8, Criteria1:=Sheets("instructions ").Range("c23").Value, Operator:=xlFilterValues
   .AutoFilter Field:=11, Criteria1:=Sheets("instructions ").Range("c35").Value, Operator:=xlFilterValues
   .AutoFilter Field:=27, Criteria1:="=*" & Sheets("instructions ").Range("c26") & "*", Operator:=xlFilterValues
End With
If Sheets("cases available ").Range("E1:E" & Usdrws).SpecialCells(xlVisible).Count = 1 Then
   MsgBox "No matches"
   Exit Sub
End If
Sheets("cases available ").Select
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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