Search code examples
excelcyclevba

VBA_How to end a For cycle in halfway and carry out the next one


Good morning,

I'm working on a quelity control report in EXCEL. I've two tables, one is my table, the other is the original table. I'd like to go through the original one and search the key information and put it into the correct cell in my report table. Here is my idea:

For each line i in my table

for each line j in the report table
    if it contains the main title and the correct zone
        for each cell k in this line j
            if there is my answer(like coulour:red)
                put it in my table
                [HERE I WANT TO QUIT THE FOR CYCLE OF k AND j AND GOTO NEXT i]
            end if
        next k
    end if
next j

next i

I've tried some methods like add a 'next j'but failed. It causes the bug. So if you've some idea please tell me.


Solution

  • You can try this algorithm:

    declare exit flag for j and set it to false
    
    For each line i in my table
    
    for each line j in the report table
        if it contains the main title and the correct zone
            for each cell k in this line j
                if there is my answer(like coulour:red)
                    put it in my table
                    set exit flag for j to true '<-- update
                    exit for                    '<-- update
                end if
            next k
        end if
        if exit flag for j is true then set it to false and exit for '<-- update
    next j
    
    next i