ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   rows that dont want to be deleted!!!! (https://www.excelbanter.com/excel-programming/285653-rows-dont-want-deleted.html)

cecilia12345[_11_]

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/


Chip Pearson

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/




Ed[_9_]

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/






Don Guillett[_4_]

rows that dont want to be deleted!!!!
 
if

Ndx=6
Range("A1:A" & Ndx).EntireRow.Delete


--
Don Guillett
SalesAid Software

"Ed" wrote in message
...
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/









All times are GMT +1. The time now is 11:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com