excel - Infinite For...Next loop: how do I fix it? -
i'm trying delete rows cell values non equivalent 1 of values array ar(). when put logical operator not loop goes infinite reason (excel freezes). in opposite works flawlessly, in case if want delete rows containing values array.
the problem on line:
if not .cells(i, 10).value = ar(j) then
my code:
sub tims() dim lastrow long, lr long dim long, j long dim t integer dim ar() string worksheets("start").activate t = count("a", range("a3:a14")) lr = range("i3:i10").end(xldown).row worksheets("master").activate sheets("master").range("a100:a" & 100 + lr - 3).value = sheets("start").range("i3:i" & lr).value worksheets("master") j = 1 lr - 2 redim preserve ar(j) ar(j) = cells(99 + j, 1) next j end worksheets("master") lastrow = .cells(.rows.count, "b").end(xlup).row end worksheets("master").activate j = 1 lr - 2 = 1 lastrow worksheets("master") if not .cells(i, 10).value = ar(j) .cells(i, 10).entirerow.delete = - 1 end if end next next j end sub
the line causing infinite loop one:
i = - 1
get rid of it, , replace this
for = 1 lastrow
with this
for = lastrow 1 step -1
i see why tried i = - 1
in order avoid skipping row each time row deleted. doesn't work: if row gets deleted, replaced empty row @ bottom of table, , reach it. empty row not contain of values in array ar(j)
, gets deleted , replaced empty row, gets deleted , replaced empty row, ad infinitum.
you have figured out had stepped through code in debug mode.
instead, iterate bottom using step -1
.
Comments
Post a Comment