Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Change the FOR loop top

Hi,
I have a For loop that delets some rows from a sheet .
The start value for the loop is 1 and the end value is the record count
which is the number of rows in the sheet. When I delete a row I have less
rows in the sheet so I decreese the record count by one and I want that the
loop will loop just as the new value of the record count not its first value.
For i=1 to recordCount
...
objWS.rows(i).Delete
recordCount=recordCount-1
next

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Change the FOR loop top

The best approach is to loop from the bottom to the top.

For xR = LastRow to 1 step -1

' your code

Next xR

To determine the Last Row use.....

Dim LastRow as Long
LastRow = Cells(Rows.Count,"A").End(xlup).Row

This will determine the last row in Column A, change his as required. You
can also use the column Number eg

LastRow = Cells(Rows.Count,1).End(xlup).Row


--
Cheers
Nigel



"DavidE" wrote in message
...
Hi,
I have a For loop that delets some rows from a sheet .
The start value for the loop is 1 and the end value is the record count
which is the number of rows in the sheet. When I delete a row I have less
rows in the sheet so I decreese the record count by one and I want that

the
loop will loop just as the new value of the record count not its first

value.
For i=1 to recordCount
...
objWS.rows(i).Delete
recordCount=recordCount-1
next

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Change the FOR loop top

If you start from the bottom and work up, you're life will be much easier:

for i = recordcount to -1 step -1
if something is true or false then
rows(i).delete
end if
next i



DavidE wrote:

Hi,
I have a For loop that delets some rows from a sheet .
The start value for the loop is 1 and the end value is the record count
which is the number of rows in the sheet. When I delete a row I have less
rows in the sheet so I decreese the record count by one and I want that the
loop will loop just as the new value of the record count not its first value.
For i=1 to recordCount
...
objWS.rows(i).Delete
recordCount=recordCount-1
next

Thanks


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Change the FOR loop top

Thanks you Dave and Nigel. Now it's works perfect

"Dave Peterson" wrote:

If you start from the bottom and work up, you're life will be much easier:

for i = recordcount to -1 step -1
if something is true or false then
rows(i).delete
end if
next i



DavidE wrote:

Hi,
I have a For loop that delets some rows from a sheet .
The start value for the loop is 1 and the end value is the record count
which is the number of rows in the sheet. When I delete a row I have less
rows in the sheet so I decreese the record count by one and I want that the
loop will loop just as the new value of the record count not its first value.
For i=1 to recordCount
...
objWS.rows(i).Delete
recordCount=recordCount-1
next

Thanks


--

Dave Peterson

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
change increasment in a for loop Jared Excel Discussion (Misc queries) 3 October 6th 06 05:54 AM
Change while loop from count to formula hotherps[_146_] Excel Programming 0 November 16th 04 03:34 PM
Change while loop from count to formula hotherps[_145_] Excel Programming 1 November 16th 04 02:34 PM
Loop to change cell color based on found value? gaba Excel Programming 3 November 3rd 04 02:33 PM
Jump out of Do Loop on sheet change Paul Excel Programming 2 May 14th 04 04:21 PM


All times are GMT +1. The time now is 05:01 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"