I have a Do Loop nested in a Do Loop nested in a For Loop (served with fries & cole slaw).
When the condition occurs to exit from the inner Do Loop (Loop #2), I want to exit from the outer Do Loop as well. To accomplish this, I use a Boolean called leave_loop and set it to True when the condition occurs in the inner loop; and the last statement of the outer Do Loop is "Loop While not leave_loop"
(Each row on Sheet1 contains a field with a "Drop Point" whose value is not unique on Sheet1. On Sheet2 is where details associated with each Drop Point (each has its own unique row o Sheet2) are listed, including Origin City. My program so far aims to step through each row on Sheet1, get the Drop Point, and look up its Origin City on Sheet2. Output, to be written to Sheet7, is a list of each city (there can be multiple Drop Points with the same Origin City) with the number of times that city has appeared (via an associated Drop Point) in Sheet 1.)
So there is a For Loop to step through 750 rows on Sheet 1, which
- gets the Drop Point
- goes to Sheet2 and
== enters a Do Loop(#1) which combs Column A for the Drop Point, and gets the Drop Point's city
== then goes to Sheet7 and enters another Do Loop(#2) which
=== Combs Column A for the city, and
=== if it finds it, increments the Send Count in Column B (and sets leave_loop to True)
=== if it doesn't find it (finds an empty cell first), enters the city name in Column A and a Send Count of 1 in Column B (and sets leave_loop to True)
=== since leave_loop is True, we're leaving Loop #1, and the For Loop can continue
Here is the code in the Do Loops. Before the program gets here, drop_point has a valid value, Sheet2 is Activated, and dp_search = 1.
Testing the code with the first 10 rows on Sheet1, I have it show a MsgBox each time it scans a Drop Point and each time it scans an Origin City. It would give me the first Drop Point, then its correct city (San Francisco, if that matters ), but then it would only show me each successive Drop Point and not their Origin City values. Sheet7 would show "San Francisco" in Column A and a correct send count of 2 in Column B. So the For Loop on Sheet1 would execute, but the outer Do Loop would execute only for one iteration and not execute again.Please Login or Register to view this content.
I hypothesized that the fact that leave_loop was set to True in the first iteration, and then never set back to False, might have been the problem. I added "leave_loop = False" just before the outer Do Loop is entered, and voilą, that fixed it.
I am confused why that should have fixed it; for the simple reason that leave_loop is to be checked at the end of the Loop, not when the Loop is entered. It's "Loop While not leave_loop"; it's not "Do While not leave_loop".
So why is the value of leave_loop affecting execution at the time the program encounters "Do"?
Bookmarks