How to count the number of lines and make it a condition when VBA inserts lines between?

advertisements

I Need to use a loop in my code and at a certain condition I will insert row.

Rowz = activesheet.Cells(Rows.Count, 1).End(xlUp).Row
for j=3 to Rowz ' say number of rowz=1000

But as rows are inserted in between So the number of rows are increased. Say for example at the 200th row I insert 4 rows and 500th row I insert 10 rows So the number is increased to more than 1000 now so the loop tests till 1000 ignoring the remaining rows which are pushed down due to inserting. But I need to perform that loop till the last row even rows are inserted in between

I thought to use these

  for j=3 to activesheet.cells(Rows.count,1).End(xlUp).Row

But the value is not updating i Guess its just checking upto 1000 only its not updating the number of rows value in the to condtion.

How do I count number of rows and keep it in a condition if rows are created in between?


Why don't you try looping backwards ? for j = rowZ to 3 step -1.
Might that solve your problem ?


Edit: option 2, you could define a name for the cell after the last one, and refer that Named Range: Range("myLastCell"). It will adjust every time you insert rows....