display userform if value of textbox not found in filtered data

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,490
Office Version
  1. 2016
Platform
  1. Windows
Hi all

Can somebody help with this VBA below

All I need is if the value added into the Textbox1 isn't found through the filtered data then display userform4

my code as follows...

VBA Code:
Private Sub CommandButton1_Click()



Sheets("Inventory").Select

With ThisWorkbook.Worksheets("Inventory")
        .AutoFilterMode = False
        With Range("B4:H1000")
            .AutoFilter
            If Len(Me.TextBox1.Value) > 0 Then
                .AutoFilter Field:=1, Criteria1:=Me.TextBox1.Value
            End If
            
            
        End With
        
        
UserForm2.TextBox1.Value = ""

UserForm2.Hide
End With
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,501
The following code uses the SpecialCells method to determine whether there are any filtered records. As such, the defined range had to be changed from a fixed range to a dynamic range.

VBA Code:
        With Range("B4:H" & Cells(Rows.Count, "B").End(xlUp).Row)
            .AutoFilter
            If Len(Me.TextBox1.Value) > 0 Then
                .AutoFilter Field:=1, Criteria1:=Me.TextBox1.Value
                On Error Resume Next
                Set filterRange = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If filterRange Is Nothing Then
                    UserForm4.Show
                    Set filterRange = Nothing
                End If
            End If
        End With

And, of course, you should declare filterRange...

VBA Code:
Dim filterRange As Range

Hope this helps!
 
Solution

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,490
Office Version
  1. 2016
Platform
  1. Windows
That's great - thanks a lot
 

Watch MrExcel Video

Forum statistics

Threads
1,129,666
Messages
5,637,649
Members
416,979
Latest member
CapeCon

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