Consulting

Results 1 to 8 of 8

Thread: How to correct this error

  1. #1
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location

    How to correct this error

    Having trouble finding a way to work around this error "subscript out of range"
    I think the error is happening because of the UBound(xx) value is 3 and the (n) value is 10. This function is part of a larger code that I am not very familiar with but hoping maybe there is a work around for this error.

    macrotroubles.jpg

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello leal72,

    You have not declared the variable n in your code.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    thanks for reply. That's not it though, I declared it to test it out and same error
    Attached Images Attached Images

  4. #4
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Guess that the value of the n variable exceeds ubound(xx).

  5. #5
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    Quote Originally Posted by 大灰狼1976 View Post
    Guess that the value of the n variable exceeds ubound(xx).
    yes, this correct. Looking for a solution that will allow code to not error out

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. Pasting the code between CODE tags would make it easier for people to test

    2. Are you sure you've converted the code correctly?

    If x = xx(n) Then
        j = 1
    ElseIf x= xx(n) Then
        j = n-1
    seems odd. Pretty sure you meant


    If x = xx(1) Then


    https://www2.units.it/ipl/students_area/imm2/files/Numerical_Recipes.pdf

    Capture.JPG


    Last edited by Paul_Hossler; 04-26-2019 at 07:59 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    Thank you for the response

    Might just be lost cause for me. Working on code for someone else, code writter no longer around to get feedback from. As code is now, it will process data for some sample sets but not for others, ie whenever the variable n value exceeds UBound(xx)

    Made the change you suggested
    HTML Code:
    If x = xx(1) Then
    gets me past this sticking point but creates another calculation issue down the line, run-time error 6, overflow
    HTML Code:
    slope = (yy2 - yy1) / (xx2 - xx1)
    and the subscript out of range still exists
    HTML Code:
    ElseIf x = xx(n) Then

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Instead of trying to port C code to VBA, it might be better to just define what you want to do, and not how you want to do it
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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