ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   search function without error if not found (https://www.excelbanter.com/excel-programming/364260-search-function-without-error-if-not-found.html)

anderssweden

search function without error if not found
 
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.

Tom Ogilvy

search function without error if not found
 
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

--
Regards,
Tom Ogilvy


"anderssweden" wrote:

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.



All times are GMT +1. The time now is 10:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com