+ Reply to Thread
Results 1 to 5 of 5

Ignore Subscript Out of Range Error

  1. #1
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Ignore Subscript Out of Range Error

    Hello,

    I am writing a binary search function. At the end I check to see if the next element matches the "search for" value. When searching for an element larger than any in the list, my marker lands on the last list element and I get a "subscript out of range" error when trying to check the non-existent next element. To me, if there is no next element then obviously it can't be equal to the value, so the if condition should fail and it should move on to the next line of code. Unfortunately, even when I put "On error resume next" it still gives me the error. How can I ignore the error and move on as if it was false?

    My code segment (inArr is the list to be searched, Match is the marker, val is the "search for value, gap/run are options):
    Please Login or Register  to view this content.
    This gives me an error if Match>UBound(inArr,1) despite the On Error Resume Next.
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Ignore Subscript Out of Range Error

    Instead of On Error Resume Next, test if Match is less than the Ubound of the array.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Ignore Subscript Out of Range Error

    Thanks AlphaFrog,

    That should work for this situation. For the future, is there a way to ignore Subscript Out of Range?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Ignore Subscript Out of Range Error

    Quote Originally Posted by k64 View Post
    Thanks AlphaFrog,

    That should work for this situation. For the future, is there a way to ignore Subscript Out of Range?
    I'd have to see the whole procedure to better understand how the variables are declared and the values defined.

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Ignore Subscript Out of Range Error

    Generally it is not wise to ignore errors :-) This could yield unexpected results
    If you are pleased with a member's answer then use the Star icon to rate it.

+ 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] Run-time error '9': subscript out of range - error occurs on multiple computers except one
    By BrettE in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2014, 11:19 PM
  2. Runtime Error - Subscript out of Range / Object Error
    By JHRice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2012, 05:14 PM
  3. Defining Array - Runtime error 9, Subscript out of range error
    By MaartenW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2012, 07:32 AM
  4. Runtime Error - Subscript out of range despite On Error statement
    By DoctorG in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 11:05 AM
  5. Subscript out of range error - save copy error
    By bg18461 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2006, 11:53 AM
  6. Type Mismatch error & subscript out of range error
    By Jeff Wright in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2005, 03:06 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