LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to continue loop after deleting row?

One way is to build a range to delete when you're done. But then you have to
adjust that range so that you can do the delete:

Option Explicit
Sub testme01()
Dim DelRng As Range
Dim myRng As Range
Dim myCell As Range

With ActiveSheet
Set myRng = .Range("a1:c9") 'selection

For Each myCell In myRng.Cells
If myCell.Value = "a" Then
If DelRng Is Nothing Then
Set DelRng = myCell
Else
Set DelRng = Union(myCell, DelRng)
End If
End If
Next myCell

If DelRng Is Nothing Then
'do nothing
Else
Intersect(DelRng.EntireRow, .Columns(1)).EntireRow.Delete
End If
End With

End Sub

====
If you did want to loop through the rows/columns, then this worked ok for me:

Option Explicit
Sub testme02()
Dim FirstRow As Long
Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iRow As Long
Dim iCol As Long
Dim myRng As Range

With ActiveSheet
Set myRng = .Range("f11:h19") 'selection

With myRng
FirstRow = .Row
LastRow = .Rows(.Rows.Count).Row
FirstCol = .Column
LastCol = .Columns(.Columns.Count).Column
End With

For iRow = LastRow To FirstRow Step -1
For iCol = FirstCol To LastCol
If .Cells(iRow, iCol).Value = "a" Then
.Rows(iRow).Delete
Exit For 'delete that row
End If
Next iCol
Next iRow
End With
End Sub




wrote:

I have the following loop:

for each cell in selection ' n-by-m area
if 'condition based on cell.value' then
cell.entireRow.delete
else
' process cell
end if
next

As written, if the range is A1:B10 and A2 meets the
condition, the loop continues by processing (the new) B2
after deleting row 2 -- which means that the new A2 is
never processed.

Is there an easy way to continue the loop by processing
(the new) A2 after deleting row 2?

I work around the problem by adding "goto again" after
the entireRow.delete operation, where "again" is above
the for-each statement. That is, I restart the for-loop.

That is okay for now because with my data, I only delete
one row near the top of the selection. Thus, I lose little
efficiency. But I would like to know the "right" way to do
this, for the future.

(I tried putting "again" at the top of loop just after the
for-each statement, but I got a debug error because
"cell" is apparently undefined after the deletion.)


--

Dave Peterson


 
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
to continue down in the same row alex178 Excel Discussion (Misc queries) 2 July 8th 06 05:34 PM
Continue Steph[_6_] Excel Programming 0 January 31st 06 05:50 PM
Deleting rows loop briskbaby Excel Programming 4 October 7th 05 09:10 PM
continue statement in a loop rozner Excel Programming 4 August 2nd 05 03:30 PM
deleting rows, endless loop maybe ? dick[_2_] Excel Programming 3 June 28th 05 06:35 PM


All times are GMT +1. The time now is 03:03 AM.

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

About Us

"It's about Microsoft Excel"