display userform if value of textbox not found in filtered data

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,979
Messages
6,122,561
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