Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a loop in a macro i'm writing of the following structure:

there are two worksheets in this book. raw data (hence the endpointData variable) and a G/L (General ledger) sheet (hence the endpointGL variable)

there are three for loops in this function:

(1) the first loop iterates through each record in the raw data file.
(2) the second loop iterates through the verified matches from REGEXP (a regular expression search) and
(3) the third loop goes through the G/L and finds a corresponding match and puts (using: PUT_DATA_RANGE) that data into the appropriate spot.

Here's sort of what i'm getting at:

psuedocode:

For i = 2 To endpointData

    If SOME_TEST = True Then
        Set MATCHES = REGEXP.EXECUTE()
        For Each myMatch In MATCHES
            For j = 1 To endpointGL
                If myMatch.value = SOME_CONDITION Then
                    PUT_DATA_RANGE
                    Exit For
                ElseIf myMatch.value <> SOME_CONDITION Then
                    MsgBox ("there might be a problem")
                    ' EXIT BOTH LOOPS HERE

                    ' write handler code
            Next
            End If
        Next
    End If
Next i

now you'll notice that i have a few comments to myself. if the third loops finds no match in the G/L the code currently interrupts to notify the user. but that message box MsgBox("there might be a problem") is looped through along with the third loop. how do i get excel to exit BOTH loops and bring the FIRST for loop to the next availabe record in the raw data?

by the way, i've tried exiting it with an Exit For but that doesn't seem to work exactly.

share|improve this question

1 Answer

I see two options.

1) Use a series of booleans to check if the outer loops should continue

2) Use a goto statement to exit from the inner loop directly back to the main procedure

In this situation, the goto might not be so bad since you aren't jumping to a completely different part of the code. Just document it well...

share|improve this answer
some sort of iterator needs to be implemented too so that it doesn't continue where it left off. right? – franklin May 10 '12 at 15:11
I don't think so. When it starts the loops the next time, they should reinitialize their counters. I would have to try it though. – vansimke May 10 '12 at 15:42
this is one way to solve it. look at the implementation of an ExitLoop boolean value. stackoverflow.com/a/10539817/778694 – franklin May 14 '12 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.