+ Reply to Thread
Results 1 to 5 of 5

Error Handling to Stop Run-time Errors

  1. #1
    Registered User
    Join Date
    06-02-2014
    Posts
    14

    Error Handling to Stop Run-time Errors

    I have a Userform that asks the user to enter a UK post code into 2 text fields. e.g

    txtField1 = L27
    txtField2 = 4HD

    I then have a Lookup function using txtField1 to search for the first part of the postcode in a list to return a result. Everything works swimmingly until an incorrect post code is entered.

    I then get a run-time error which is not ideal for the user.

    Is there a way I could write some error handling code that tells the user an invalid post code has been entered rather than getting the run-time error?

    My Lookup function is here:

    Please Login or Register  to view this content.
    This is the current error handling I have in place but this obviously doesn't pick up if an invalid post code has been entered.

    Please Login or Register  to view this content.
    If there is any standard error handling code to effectively by-pass the run-time error then this should work fine.

    Thanks in advance.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Error Handling to Stop Run-time Errors

    You could do a countif on the postcode make sure its in the table? You can trap what you have by if len(txtPost1.value)=0 then, NullString is vbNullString also.

    Or you can use error trapping, i'd recommend reading up on this.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Error Handling to Stop Run-time Errors

    I'd suggest Application.Match instead:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    06-02-2014
    Posts
    14

    Re: Error Handling to Stop Run-time Errors

    Hi Romperstomper

    That code has worked perfectly!

    Many thanks for your help


    nathansav,

    Thanks for your suggestion. Error trapping will come in useful as I get deeper and deeper into the world of VBA so I will be doing some reading up on this as you suggested.

    Many thanks

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Error Handling to Stop Run-time Errors

    Do, as you can use errors to your advantage, so for things like a function to see if a worksheet exists, rather than loop the sheets, you can just error trap setting the sheet to an object, where the error trap returns false, and the error free code will return true.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] error handling (works first time but not the second)
    By apenca in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-26-2014, 12:04 PM
  2. [SOLVED] Stop VBA errors - On error do nothing
    By ExcelGal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2012, 11:39 AM
  3. [SOLVED] Insufficient Time Error Handling
    By Jenn68 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-06-2012, 07:16 PM
  4. Error Handling with Multiple Errors
    By Caligula in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-19-2008, 04:40 PM

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