Can I add "Error handling" if there is no match in this formula?

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
I saw this from VBA Express. [FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]http://www.vbaexpress.com/forum/showthread.php?48063-UserFrom-TextBox-search-in-ListBox-in-the-same-UserForm


It was very helpful, I was wondering if you can add error handling if you find no match?

Private Sub TextBox1_Change()Dim i As Long
Dim sFind As String

sFind = Me.TextBox1.Text

If Len(sFind) = 0 Then
Me.ListBox1.ListIndex = -1
Me.ListBox1.TopIndex = 0
Else
For i = 0 To Me.ListBox1.ListCount - 1
If UCase(Left(Me.ListBox1.List(i), Len(sFind))) = UCase(sFind) Then
Me.ListBox1.TopIndex = i
Me.ListBox1.ListIndex = i
Exit For
End If
Next i
End If
End Sub

Thanks
<strike></strike>
[/FONT]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Is this what you want?

Code:
Private Sub TextBox1_Change()
    Dim i As Long
    Dim sFind As String
    
    sFind = Me.TextBox1.Text
    
    If Len(sFind) = 0 Then
        Me.ListBox1.ListIndex = -1
        Me.ListBox1.TopIndex = 0
    Else
        For i = 0 To Me.ListBox1.ListCount - 1
            If UCase(Left(Me.ListBox1.List(i), Len(sFind))) = UCase(sFind) Then
                Me.ListBox1.TopIndex = i
                Me.ListBox1.ListIndex = i
                Exit For
            Else
                Me.ListBox1.ListIndex = -1
                Me.ListBox1.TopIndex = 0
            End If
        Next i
    End If
End Sub
 

Forum statistics

Threads
1,143,676
Messages
5,720,242
Members
422,272
Latest member
ginkgoVil

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