+ Reply to Thread
Results 1 to 9 of 9

Referencing a counting variable within a recursive procedure

  1. #1
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Referencing a counting variable within a recursive procedure

    If I have a subroutine that is recursive and I want to keep track of the number of values found as it recursively calls itself, how can I do that? The counter resets after each recursive call.

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

    Re: Referencing a counting variable within a recursive procedure

    Declare the variable at the top of the code module so it is persistent
    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
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Referencing a counting variable within a recursive procedure

    You could recode as a function and make it the return value, For example,

    Please Login or Register  to view this content.
    A
    B
    C
    1
    XFD
    16384
    B1: =colnum(A1)
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Referencing a counting variable within a recursive procedure

    Quote Originally Posted by AlphaFrog View Post
    Declare the variable at the top of the code module so it is persistent
    AlphaFrog - This works first time through, but if I run it again, it seems to add on to previous value. I must need to set the variable back to zero somewhere?

  5. #5
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Referencing a counting variable within a recursive procedure

    Here is the code I am running. It does the calculation correctly, but I want to store each solution in an array. I'm trying to store each solution in an array so I need to keep track of and increment array index as solutions are found. Since I declared the index variable as global, the variable needs to be reset to zero once the process has completed so the count is accurate next time it initiates. Once I have the solutions stored in the array, I will make the sub into a function that returns this array. On further inspection, it appears that maybe the problem is the program is still running even though it has finished. If I actually end it myself then re-run it, it works. But if I don't manually end it, it crashes because it continues on with the index count from the previous run.
    Please Login or Register  to view this content.
    Last edited by bvwalker1; 01-30-2019 at 04:05 PM.

  6. #6
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Referencing a counting variable within a recursive procedure

    Quote Originally Posted by shg View Post
    You could recode as a function and make it the return value, For example,

    Please Login or Register  to view this content.
    A
    B
    C
    1
    XFD
    16384
    B1: =colnum(A1)
    shg - I'm not sure I follow what you are doing.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Referencing a counting variable within a recursive procedure

    Here's a pretty terrible way to subtract two numbers using recursion by modifying an argument:

    Please Login or Register  to view this content.
    Last edited by shg; 01-30-2019 at 05:00 PM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Referencing a counting variable within a recursive procedure

    Here's the same thing using the return value of the function:

    Please Login or Register  to view this content.
    Last edited by shg; 01-30-2019 at 05:07 PM.

  9. #9
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Referencing a counting variable within a recursive procedure

    I figured it out. I used static variables and preserve keyword.
    Please Login or Register  to view this content.

+ 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] call variable from one procedure to in other procedure
    By shiva_raj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-20-2017, 02:19 AM
  2. How to use a Define Name variable in VBA procedure
    By davebliz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2014, 05:13 AM
  3. Using a variable calculated in a procedure in another procedure.
    By Trinidad3 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-06-2010, 04:51 AM
  4. determine a variable's value then use it in another procedure
    By carsto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2007, 06:33 PM
  5. Procedure name as variable
    By Redbeard in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-02-2006, 06:20 AM
  6. [SOLVED] Variable procedure call
    By Art in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-16-2005, 09:20 AM
  7. [SOLVED] Call procedure using variable
    By donbowyer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2005, 05:05 AM

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