search function without error if not found

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a search function that I intend to use for several variables. I wonder
how you write the search function so that you do not get error if search is
unsuccessful but rather an informative message box or likewise. My code so
far is:
Private Sub findQC()
Dim rng As Range
Dim findVal As String
Dim i As Integer
Set rng = Worksheets("Dimension").Cells.Find("qc", LookIn:=xlValues)
i = 0
Do Until IsEmpty(rng.Offset(i + 1, 0)) = True
i = i + 1
Loop
End Sub

It is the Set rng = ... that fails if search is unsucessful. Very thakful
for fast assistance.
 
You are incorrect.

Set rng = Worksheets("Dimension").Cells.Find("qc", LookIn:=xlValues)

does not raise an error if the target is not found. Assuming that it
contains a reference later in your code is probably the source of your problem

Private Sub findQC()
Dim rng As Range
Dim findVal As String
Dim i As Integer
Set rng = Worksheets("Dimension").Cells.Find("qc", LookIn:=xlValues)
if not rng is nothing then
i = 0
Do Until IsEmpty(rng.Offset(i + 1, 0)) = True
i = i + 1
Loop
else
msgbox "QC was not found"
end if
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top