+ Reply to Thread
Results 1 to 2 of 2

search function without error if not found

  1. #1
    anderssweden
    Guest

    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.

  2. #2
    Tom Ogilvy
    Guest

    RE: 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.


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1