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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
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
 

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74
How do I re-write this line so it doesn't error out?

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

Watch MrExcel Video

Forum statistics

Threads
1,130,168
Messages
5,640,543
Members
417,151
Latest member
ChickenTenderer

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