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.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

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