+ Reply to Thread
Results 1 to 6 of 6

Output result from a recursive function.

  1. #1
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Output result from a recursive function.

    The main purpose of the script is to pad, 99 with the value in front of it in the first column, where it was derived from, then later on, the value 100 will substituted in its place.

    I came up with a Do While Loop that successfully completed this task.

    Please Login or Register  to view this content.
    As indicated in the comment of the second If statement, the recursion works, I just need it exit the function and send the string value back to main sub.

    Please Login or Register  to view this content.
    The following in the main sub returns nothing
    Please Login or Register  to view this content.
    Edit: Theere was a misplaced comma in the if-statement, note the post was "unintentionally updated". Sorry.
    Attached Files Attached Files
    Last edited by excelicus; 08-29-2018 at 05:34 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Output result from a recursive function.

    I am a little confused, because the function code in your sample file is a little different from what you have in your post. In your sample file, the innermost block If is
    Please Login or Register  to view this content.
    As my comments in the code explain, it seemed to work just fine for me if I replace the Call statement in the sample file with the Let statement in your post code. I think this works because, when you use the Call statement to call a function, the return value is discarded, so the final function call assigns the value, but that value is not carried up through the stack of function calls to the original function call.

    Is the solution as simple as making sure you are using the same version of the code in the sample file as you are posting, or is there something else going on that I am missing?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Re: Output result from a recursive function.

    This is the complete code of what I was trying to achieve

    Please Login or Register  to view this content.
    Using
    Call recursionSourceVeh(Left(sourceVeh, Len(sourceVeh) - 1), os, vn, myWs)

    and

    Call truncateSourceVeh(Left(sourceVeh, Len(sourceVeh) - 1), os, myWs)

    In both else statements returned the wrong value.

    I got '24099JB100JB, instead of '24100JB

    The 2nd function truncate, doesn't return anything.
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Output result from a recursive function.

    I'm a little confused. You've marked this solved, but there seems to be more question.

    When I run the code (without edits, except adding Stop statements at convenient points, like at the top of the truncate function), the truncate function returns 24 (the left two characters from the input string). As near as I can tell, this is what you are telling it to do. What am I missing?

  5. #5
    Registered User
    Join Date
    12-20-2014
    Location
    North America
    MS-Off Ver
    2013
    Posts
    40

    Re: Output result from a recursive function.

    I've gone ahead and included the statements in Line 61 & Line 71.

    Commenting the statements above them, and executing them (the 'Call') in their place, generates error 5.

    I marked it as solved, because I have a recursion statement that works; I misplaced a parentheses.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Output result from a recursive function.

    I put a stop statement right before the Dim tempsourceveh... statement. I then ran through the code as is and checked the variables at the stop statement. Then I stepped through the remaining statements and watched the variables change. Right before the "error" statement, I could see that sourceveh contained "24099JB" and tempsourceveh contained "24100".
    Then I switched the comment statements so that it would run the Call statement versions and ran the code again. Checked the variables at the stop statement, stepped through the truncate function, and checked the variables before executing the "error" statement. sourceveh still contained "24099JB", but tempsourceveh now contained "24099JB100". Thinking through the error statement, it was obvious that Len(sourceveh)-Len(tempsourceveh) would be a negative number, and the Right() function does not know what to do with a negative number for the number of characters argument. That is the immediate cause for the error 5. Further debugging would involve understanding why the truncate function returns a longer string than the input string, which, because of the recursion, is probably rooted in the fact that, when the Call keyword is used to call a function statement, the return value is discarded (as explained in the help file for the Call statement: https://docs.microsoft.com/en-us/off...call-statement ).

    Does that help at all?

+ 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. Max Date using recursive function
    By karthick_s09 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-29-2016, 02:56 AM
  2. [SOLVED] Recursive factorial function
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2015, 06:24 AM
  3. [SOLVED] Excel Recursive function or not..! Pl help
    By xsmash in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 09-13-2013, 09:17 PM
  4. Recursive function that updates columns
    By lundy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-06-2012, 04:25 PM
  5. writing a recursive function
    By rmoney in forum Excel General
    Replies: 6
    Last Post: 07-19-2010, 02:59 PM
  6. Recursive/Running Function
    By goldm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-07-2009, 08:34 AM
  7. [SOLVED] recursive output if amount > x
    By benjo4u in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-19-2005, 05:05 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