Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
rows that dont want to be deleted!!!!
i have this code:
lets say numfilas is 6 i want to delete the first *6* rows Sheets(2).Select For i = 1 To NumFilas Range("a1").EntireRow.Delete Next i it deletes some rows but some are just not deleted... why? i have tried also cells(i,1).entirerow.delete and row(i).delete --- Message posted from http://www.ExcelForum.com/ |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
rows that dont want to be deleted!!!!
Cecilia,
The reason is that when you delete row 1, what used to be row 2 now becomes row 1. Then the loop variable is incremented from 1 to 2, and the new row 2 (which used to be row 3) is deleted, leaving the original row 2 (now row 1) in place. This behavior repeats itself with each iteration of the loop. When deleting rows in a loop, you should go in descending order: Dim Ndx As Long For Ndx = 6 To 1 Step -1 Rows(Ndx).Delete Next Ndx You could do this without a loop, of course. Range("A1:A6").EntireRow.Delete -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "cecilia12345" wrote in message ... i have this code: lets say numfilas is 6 i want to delete the first *6* rows Sheets(2).Select For i = 1 To NumFilas Range("a1").EntireRow.Delete Next i it deletes some rows but some are just not deleted... why? i have tried also cells(i,1).entirerow.delete and row(i).delete --- Message posted from http://www.ExcelForum.com/ |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
rows that dont want to be deleted!!!!
Range("A1:A6").EntireRow.Delete
Would that work as: Range("A1:A" & Ndx).EntireRow.Delete ?? Ed "Chip Pearson" wrote in message ... Cecilia, The reason is that when you delete row 1, what used to be row 2 now becomes row 1. Then the loop variable is incremented from 1 to 2, and the new row 2 (which used to be row 3) is deleted, leaving the original row 2 (now row 1) in place. This behavior repeats itself with each iteration of the loop. When deleting rows in a loop, you should go in descending order: Dim Ndx As Long For Ndx = 6 To 1 Step -1 Rows(Ndx).Delete Next Ndx You could do this without a loop, of course. Range("A1:A6").EntireRow.Delete -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "cecilia12345" wrote in message ... i have this code: lets say numfilas is 6 i want to delete the first *6* rows Sheets(2).Select For i = 1 To NumFilas Range("a1").EntireRow.Delete Next i it deletes some rows but some are just not deleted... why? i have tried also cells(i,1).entirerow.delete and row(i).delete --- Message posted from http://www.ExcelForum.com/ |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Cntrl-end goes beyond end after rows have been deleted ? | Excel Discussion (Misc queries) | |||
How do I delete checkboxes from rows I deleted in a macro? | Excel Discussion (Misc queries) | |||
Accidently deleted rows in Excel. | Excel Discussion (Misc queries) | |||
Locking a couple rows so when you do a delete shift row they dont | Excel Discussion (Misc queries) | |||
how do i delete the extra empty rows and columns that i dont need. | Excel Discussion (Misc queries) |