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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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