VBA order solution

redspanna

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

I'm getting myself confused and can't seem to get this code to work in correct order....

VBA Code:
Private Sub CommandButton1_Click()
Dim filterRange As Range
TextBox1.SetFocus
Application.ScreenUpdating = False
Sheets("Database").Select
 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
                    UserForm2.Hide
                    UserForm3.Show
                    Set filterRange = Nothing
                End If
            End If
            Me.TextBox1.Value = ""
        UserForm2.Hide
        Sheets("Front").Select
        UserForm4.Show
        End With
        
        Application.ScreenUpdating = True
End Sub


What I would like it to happen is following....

The user is asked to input a string of data to be searched for into Textbox1 of the userform
This value is then searched for using a filter from the data stored in the "Database" sheet.
IF found then userform2 should be hidden and userform4 displayed on the "front" sheet..
IF NOT found then hide userform2 and display userform3

Hope this makes senses and thanks in advance
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

fadee2

Active Member
Joined
Nov 7, 2020
Messages
336
Office Version
  1. 2019
Platform
  1. Windows
try the following
VBA Code:
Private Sub CommandButton1_Click()
Dim filterRange As Range
TextBox1.SetFocus
Application.ScreenUpdating = False
Sheets("Database").Select
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
                    Unload Me
                    UserForm3.Show
                    Set filterRange = Nothing
                 Else
                    Unload Me
                    UserForm4.Show
                End If
            End If
            Me.TextBox1.Value = ""
        Sheets("Front").Select
        End With
        Application.ScreenUpdating = True
End Sub

OR

VBA Code:
Private Sub CommandButton1_Click()
Dim filterRange As Range
TextBox1.SetFocus
Application.ScreenUpdating = False
Sheets("Database").Select
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
                        UserForm3.Show
                        UserForm2.Hide
                    Set filterRange = Nothing
                 Else
                    UserForm4.Show
                    UserForm2.Hide
                End If
            End If
            Me.TextBox1.Value = ""
        Sheets("Front").Select
        End With
        Application.ScreenUpdating = True
End Sub

Hide vs Unload:
Hide: simply hides the userform, it is still there, we just cannot see it.
Unload: Closes the form completely.

Hope that answers the question
 

Watch MrExcel Video

Forum statistics

Threads
1,128,099
Messages
5,628,682
Members
416,332
Latest member
blkbeltmjk

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