Macro to display a message box if autofilter results in zero visible rows

dal1503

New Member
Joined
Jan 10, 2017
Messages
11
Hi all,

I'm working on a macro to autofilter a column based on text entry from the user. I've managed to get this to work, but I want a message saying "No Records Found" to be displayed if the filter results in zero visible rows i.e. the record that the user has entered/searched for does not exist.

This is my code so far:

Code:
Sub SearchFamilyIDTest()


    Dim searchBox As String
    
    On Error GoTo Errorcatch
    
    searchBox = InputBox("Please Enter a Family to Search For:")
    If searchBox = vbNullString Then Exit Sub


    Sheet1.Range("$A$1:$J$102").AutoFilter Field:=1, Criteria1:=searchBox
    
    Exit Sub
    
Errorcatch:
        MsgBox Err.Description
    
End Sub

Any help would be appreciated. Please let me know if there's any information I've missed that you need.

Thanks.
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Fennek

Active Member
Joined
Nov 21, 2016
Messages
279
Hi,

try

Code:
with Sheet1.Range("$A$1:$J$102")
.AutoFilter Field:=1, Criteria1:=searchBox
if .count = 1 then msgbox "no visible" 'header assumed
.autofilter
end with

regards
 

dal1503

New Member
Joined
Jan 10, 2017
Messages
11
Hi,

try

Code:
with Sheet1.Range("$A$1:$J$102")
.AutoFilter Field:=1, Criteria1:=searchBox
if .count = 1 then msgbox "no visible" 'header assumed
.autofilter
end with

regards

Thanks for your suggestion, however I got a Compile error: Syntax error when I tried that
 

Watch MrExcel Video

Forum statistics

Threads
1,123,514
Messages
5,602,093
Members
414,501
Latest member
mdhaumyu

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
Top