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.
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.
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.
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 1XFD 16384B1: =colnum(A1)
Entia non sunt multiplicanda sine necessitate
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.
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.
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.
I figured it out. I used static variables and preserve keyword.
Please Login or Register to view this content.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks