Results 1 to 4 of 4

Thread: [RESOLVED] Form error in excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Location
    Oxford UK
    Posts
    76

    Resolved [RESOLVED] Form error in excel

    Hi, I am a total newbie and I'm playing around with a little form where you enter a number then it finds and displays the appropriate info. It works ok apart from when you put the wrong number in, then it comes up with an error.
    You'll have to have a look at it to understand what I mean.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Form error in excel

    Quote Originally Posted by thelocaluk
    Hi, I am a total newbie and I'm playing around with a little form where you enter a number then it finds and displays the appropriate info. It works ok apart from when you put the wrong number in, then it comes up with an error.
    You'll have to have a look at it to understand what I mean.
    You code in the Find Button should test the rCell object like this
    VB Code:
    1. Set rCell = Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    2.         xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    3.         )
    4.     If rCell Is Nothing Then
    5.         bFound = False
    6.         MsgBox "Sorry, no matches", vbOKOnly
    7.     Else
    8.         bFound = True
    9.         ListBox1.AddItem rCell(1, 2).Cells
    10.         ListBox2.AddItem rCell(1, 3).Cells
    11.     End If
    And why are you disabling the Error handler? It is always a good idea to have the Error Handler code in your procedure.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Form error in excel

    If you read the help file for the Find Method, it says "Returns Nothing if no match is found". You can use this to trap for those situations where the user enters an invalid Job Id.
    To do this, you should change your IF statement from
    VB Code:
    1. If TextBox1 Like rCell Then
    2.         bFound = True
    3.         ListBox1.AddItem rCell(1, 2).Cells
    4.         ListBox2.AddItem rCell(1, 3).Cells
    5.     Else
    6.         bFound = False
    7.         MsgBox "Sorry, no matches", vbOKOnly
    8.     End If
    to
    VB Code:
    1. If rCell Is Nothing Then
    2.         bFound = False
    3.         MsgBox "Sorry, no matches", vbOKOnly
    4.     Else
    5.         bFound = True
    6.         ListBox1.AddItem rCell(1, 2).Cells
    7.         ListBox2.AddItem rCell(1, 3).Cells
    8.     End If
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Location
    Oxford UK
    Posts
    76

    Re: Form error in excel

    Thanks guys, it all makes perfect sense when you see it.

Posting Permissions

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



Click Here to Expand Forum to Full Width