excel - exit for and keep looping the upper level for -
i want keep looping outside after exiting inside in if statement, logic should right don't know how code it. did , gives me "next without for" error. ideas?
here code:
for inrcounter = 2 inrnum exrcounter = 2 exrnum 'add missing column data input sheet existing sheet lastcofthisr = sheet1table.cells(exrcounter, columns.count).end(xltoleft).column 'searching address if sheet1table.cells(inrcounter, 1) = sheet2table.cells(exrcounter, 1) , sheet1table.cells(inrcounter, 2) = sheet2table.cells(exrcounter, 2) , sheet1table.cells(inrcounter, 3) = sheet2table.cells(exrcounter, 3) if lastcofthisr < incnum lastcofrowcounter = lastcofthisr + 1 incnum sheets("sheet1").cells(exrcounter, lastcofrowcounter) = sheets("sheet2").cells(inrcounter, lastcofrowcounter) next lastcofrowcounter 'found match loop next input row exit next inrcounter else 'do nothing end if else next exrcounter 'do nothing end if 'did not find input row, find last row of existing , add lrowex = sheets("sheet1").cells.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row countercoflastr = 1 incnum sheets("sheet1").cells(lrowex + 1, countercoflastr) = sheets("sheet2").usedrange.columns(1).cells(inrcounter, countercoflastr) next countercoflastr next inrcounter
you cannot start for
loop , have next
statement embedded in if
statement.
example:
for = 0 10 if range("a1").value = "" next end if
will throw error because next inside if statement. proper way be:
for = 0 10 if range("a1").value = "" end if next
it looks have 1 many next statements.
edit:
to jump out of loop can use exit for
for example:
for = 0 10 x = 0 10 exit debug.print x next debug.print next
only i
printed 0 10 since inner loop hit exit for
if want skip loop iteration can manually increment loop counter. example following code skip 5
because i
increased @ beginning of loop.
for = 0 10 if = 5 = + 1 end if debug.print next
Comments
Post a Comment