VBA order solution

redspanna

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

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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