View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Nick Hodge Nick Hodge is offline
external usenet poster
 
Posts: 1,173
Default loop to delete rows...

The trick when deleting rows is to index backwards (Step - 1), so the
following code, should help you on your way

Sub deleteTotalSubTotal()
Dim lbottom As Long
Dim x As Long
lbottom = Range("A65536").End(xlUp).Row
For x = lbottom To 1 Step -1
If InStr(1, Range("A" & x).Value, "SubTotal") Or _
InStr(1, Range("A" & x).Value, "Total") Then
Range("A" & x).EntireRow.Delete
End If
Next x
End Sub

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


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