VBA Find Function Error

MarkRCC

New Member
Joined
May 22, 2017
Messages
46
Hi All,

I have a userform that has a label that, when clicked, will take the user to the respective sheet and select the cell of what they have entered into a text field. Essentially just a automatic Ctrl+F button.

The code I am using works fine when the value exists but crashes when an non-existent value is entered. I knew this was going to be a problem, but I don't know what to add to the code to make it ignore/tell the user it was not found, rather than just crashing completely.

Code:
Private Sub Label3_Click()
ThisWorkbook.Sheets("HS").Activate
ActiveWorkbook.Worksheets("HS").Columns(1).Find(TextBox1.Value).Select
End Sub

How can I fix my problem? Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
One way to handle that run-time error is to find a cell (range object) with the value you want in it. If no such cell exists, the range object will be Nothing and your code can react to that. If a cell is found the code has set a range object variable to be that cell and you can do whatever you wish with it (e.g. select it). Like this:
Code:
Private Sub Label3_Click()
Dim FoundRng As Range
With Sheets("HS")
    Set FoundRng = .Columns(1).Find(textbox1.Value)
    If Not FoundRng Is Nothing Then
        .Activate
        FoundRng.Select
    Else
        MsgBox "Cannot find " & textbox1.Value & " in column A"
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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