excel - Find a Value and insert a row in VBA -
sub o() dim x integer dim y integer y = 2 2 x = 1 600 if cells(x, y).value = "cd sector average" cells(x, y).entirerow.select selection.insert shift:=xldown cells(x + 2, y).select end if next x next y y = 2 2 x = 1 600 next x if cells(x, y).value = "cs sector average" cells(x, y).entirerow.select selection.insert shift:=xldown cells(x + 2, y).select end if next x next y y = 2 2 x = 1 600 next x if cells(x, y).value = "e sector average" cells(x, y).entirerow.select selection.insert shift:=xldown cells(x + 2, y).select end if next x next y end sub
so looking find first value , add row above , continue searching next value , add row.
what inserts rows before first value until hits 600 range macro ends.
how can altered run find , insert 1 time , move next value find?
the problem you're working in wrong direction. loop, if finds value in row 5 (x = 5), example, add row , shift row value found down (in case 5 6). when loop iterates, moves next row (x = 6) , finds same exact row moved.
fix this, need work backwards. practice when performing function this, or when deleting specific rows. work backwards, need make small change loop. try changing to:
for x = 600 1 step -1
tells excel go 600 1, iterating -1 @ time, avoiding problem you're seeing.
Comments
Post a Comment