loop, find+delete

Seamus2626
Seamus2626 used Ask the Experts™
on
Hi,

I need some code that can loop through Column B and look for

"STANDARD DIVIDEND PAYMENT"

Once found delete that row and move down and keep deleting rows until you find

"NOTICE==================="

Once this is found, delete that row as well but sop deleting rows, then restart the above process until it has looped through the count of ColA

Hope that makes sense!

Thanks
Seamus
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
JPIT Director

Commented:
It is a little unclear, you say stop deleting rows and restart the above process which deletes rows??
Seamus - when you restart the process what exactly needs to be done?
JPIT Director

Commented:
OK, I read though it again and I think I know what your asking for. Couple minutes I can shoot you something.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ditto, but will leave to you peetjh!

Author

Commented:
When i say restart the process i mean start looking for

"STANDARD DIVIDEND PAYMENT"
 again, once found delete the row, and keep deleting until

"NOTICE==================="

and then go again!

Thanks
Seamus
JPIT Director

Commented:
Try this code attached. It starts at the bottom and works to the top because when you delete rows they shift up and you would end up skipping rows.
Sub cleanup()
Dim lastrow As Long, noticerow As Long, paymentrow As Long
lastrow = [a65536].End(xlUp).Row
For i = lastrow To 2 Step -1 'Assumes you have a header row. If not change "to 2" to "to 1"
    If Cells(i, "a").Value = "NOTICE===================" Then
        noticerow = i
    End If
    If Cells(i, "a").Value = "STANDARD DIVIDEND PAYMENT" Then
        paymentrow = i
        Rows(paymentrow & ":" & noticerow).Delete (xlUp)
    End If
Next i
End Sub

Open in new window

JPIT Director

Commented:
If the number of "=" after NOTICE is not consistent we could replace line 5 with.

If mid(Cells(i, "a").Value,1,6) = "NOTICE" Then

Author

Commented:
That looks good Peetjh, but i get type mismatch errors because on some rows i have #NAME errors, can the code ignore those cells with #NAME?

Thanks
Seamus
IT Director
Commented:
Revised code attached.
Sub cleanup()
Dim lastrow As Long, noticerow As Long, paymentrow As Long
lastrow = [a65536].End(xlUp).Row
For i = lastrow To 2 Step -1 'Assumes you have a header row. If not change "to 2" to "to 1"
    On Error GoTo continue
    If Cells(i, "a").Value = "NOTICE===================" Then
        noticerow = i
    End If
    If Cells(i, "a").Value = "STANDARD DIVIDEND PAYMENT" Then
        paymentrow = i
        Rows(paymentrow & ":" & noticerow).Delete (xlUp)
    End If
continue:
Next i
End Sub

Open in new window

Top Expert 2010
Commented:
The following seems to be working for me, using the Find method, which is usually faster than looping through a range:

Sub KillRows()
    
    Dim SRng As Range, DRng As Range
    
    Const SText As String = "STANDARD DIVIDEND PAYMENT"
    Const DText As String = "NOTICE==================="

    Do
        Set SRng = Range("b:b").Find(SText, Range("b" & Rows.Count), xlValues)
        If Not SRng Is Nothing Then
            Set DRng = Range("b:b").Find(DText, SRng, xlValues)
            If Not DRng Is Nothing Then
                Range(SRng, DRng).EntireRow.Delete
            Else
                Exit Do
            End If
        Else
            Exit Do
        End If
    Loop
    
    MsgBox "Done"
    
End Sub

Open in new window

Author

Commented:
Thanks guys!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial