excel - VBA insert new rows if 2 conditions met -
what i'm trying achieve insert number of new rows after rows contain in column text "rich", , if column b in same row contains value less 10 insert 2 rows after row. if column b in same row contain value higher insert 1 row after row. i'm not best in writing loop code. appreciate help.
i managed after time :)
sub macro1() range("a1").select ' remember last row contains value dim lastrow integer dim currentrow integer lastrow = range("a1").end(xldown).row currentrow = 1 ' keep on incrementing current row until ' past last row while currentrow <= lastrow ' if desired string found, insert row above ' current row ' means, our last row going 1 more ' row down ' , means, must double-increment our current ' row if range("a" & currentrow).value = "rich" , range("b" & currentrow).value > 10 range("a" & currentrow + 1).entirerow.insert xlip range("a" & currentrow + 1).entirerow.insert xlip lastrow = lastrow + 2 currentrow = currentrow + 1 elseif range("a" & currentrow).value = "rich" , range("b" & currentrow) < 10 range("a" & currentrow + 1).entirerow.insert xlup lastrow = lastrow + 1 currentrow = currentrow + 1 end if ' put pointer next row want evaluate currentrow = currentrow + 1 loop end sub
Comments
Post a Comment