Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 194
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cntrl-end goes beyond end after rows have been deleted ? Domrob Excel Discussion (Misc queries) 2 December 7th 06 04:09 PM
How do I delete checkboxes from rows I deleted in a macro? Jazzi-D Excel Discussion (Misc queries) 1 January 18th 06 12:49 AM
Accidently deleted rows in Excel. Pelochino Excel Discussion (Misc queries) 2 October 13th 05 09:40 PM
Locking a couple rows so when you do a delete shift row they dont palukagoat Excel Discussion (Misc queries) 3 March 22nd 05 05:25 PM
how do i delete the extra empty rows and columns that i dont need. lindaY Excel Discussion (Misc queries) 3 March 20th 05 03:48 PM


All times are GMT +1. The time now is 09:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"