View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Frank Kabel Frank Kabel is offline
external usenet poster
 
Posts: 3,885
Default loop to delete rows...

Hi
the problem for this kind of operation is that you have to work from
the bottom to the top (not vice versa). e.g. try
Sub delete_rows()
Dim lastrow As Long
Dim row_index As Long
Application.ScreenUpdating = False
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For row_index = lastrow To 1 Step -1
If instr(cells(row_index,1).value, "Total")0 " then
Rows(row_index).delete
End If
Next
Application.ScreenUpdating = True
End Sub

--
Regards
Frank Kabel
Frankfurt, Germany


Froglegz wrote:
here's the issue: I'm trying to delete all the rows which
start with "total:" and "subtotal:" in column A. I had to
use 2 loops because when there is a subtotal row directly
followed by a total row, the total row is "skipped". I'm
not sure that I'm making myself very clear but any help
would be appreciated... Ideally I'd like to replace the 2
loops by 1.


Here's what I have so far:

sub delRows()
Dim c As Range
For Each c In Range(Range("a1"), Range("a1").End
(xlDown)).Cells
If c.Text Like "SubTotal*" Then
c.EntireRow.Delete
End If
Next c

For Each c In Range(Range("a1"), Range("a1").End
(xlDown)).Cells
If c.Text Like "Total*" Then
c.EntireRow.Delete
End If
Next c
end sub