Autofilter - no cells were found

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74
How can I modify this code so that a message box pops up when the criteria is not found and avoid getting the error "no cells were found"?

Code:
Private Sub tbJobNo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
        Dim xVal As Variant
        Worksheets("part number").AutoFilterMode = False
        With Worksheets("part number").Range("Job_No")
            .AutoFilter Field:=1, Criteria1:=frmDQT.tbJobNo.Value
            If .Range("Job_No") Is Nothing Then
            Exit Sub
            frmDQT.cboPN.Clear
            For Each xVal In .Columns(2).SpecialCells(xlCellTypeVisible)
                frmDQT.cboPN.AddItem xVal.Value
            Next xVal
            .AutoFilter
        End With
    End If
    ActiveSheet.Rows("3:3").Hidden = True
    tbPartNo = ""
End Sub
Thanks,
CEG
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Not sure but have u tried adding error handler?
Code:
[/FONT]
[FONT=Courier New]Private Sub tbJobNo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
        Dim xVal As Variant
        Worksheets("part number").AutoFilterMode = False
        With Worksheets("part number").Range("Job_No")
            .AutoFilter Field:=1, Criteria1:=frmDQT.tbJobNo.Value[/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]on error resume next
            If .Range("Job_No") Is Nothing Then[/FONT]
[FONT=Courier New]msgbox "Not found...": Exit Sub[/FONT]
[FONT=Courier New]on error goto 0
            frmDQT.cboPN.Clear
            For Each xVal In .Columns(2).SpecialCells(xlCellTypeVisible)
                frmDQT.cboPN.AddItem xVal.Value
            Next xVal
            .AutoFilter
        End With
    End If
    ActiveSheet.Rows("3:3").Hidden = True
    tbPartNo = ""
End Sub
 
Upvote 0
How do I re-write this line so it doesn't error out?

Code:
If .Range("Job_No") Is Nothing Then
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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