Excel VBA Delete Rows -
i trying create program delete or create rows based on user puts in row. example, if user puts in 3, there 2 rows, insert 1 row. if there 5 rows, delete rows 4 , 5. seems should easy code, having hardest time having delete/create rows want to. code follows:
sheets("summary").select x = cells(29, 3).value = 7 sheets("weighted i").select until cells(i, 1).value = "total" = + 1 loop = - 7 if > x dlt = - x + 7 cnt = 7 dlt rows(cnt).entirerow.delete cnt = cnt + 1 next elseif < x crt = x - + 7 cnt = 7 dlt rows(cnt).entirerow.insert cnt = cnt + 1 next end if
this common problem when deleting rows. imagine moving through loop 1 row @ time , deleting:
for cnt = 7 dlt rows(cnt).entirerow.delete cnt = cnt + 1 next
you on row 7 , delete it. shifts of rows up. row 8 row 7. increase cnt
variable 1 (to 8) , delete row 8. missed row 7, row 8... it's crazy bananas.
instead, change loop work backwards:
for cnt = dlt 7 step -1 rows(cnt).entirerow.delete cnt = cnt - 1 next
this way row shifting doesn't affect cnt
.
Comments
Post a Comment