Solved

loop, find+delete

Posted on 2011-09-02
11
272 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 14

Expert Comment

by:peetjh
ID: 36473194
It is a little unclear, you say stop deleting rows and restart the above process which deletes rows??
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36473201
Seamus - when you restart the process what exactly needs to be done?
0
 
LVL 14

Expert Comment

by:peetjh
ID: 36473203
OK, I read though it again and I think I know what your asking for. Couple minutes I can shoot you something.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:StephenJR
ID: 36473209
Ditto, but will leave to you peetjh!
0
 

Author Comment

by:Seamus2626
ID: 36473238
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
0
 
LVL 14

Expert Comment

by:peetjh
ID: 36473269
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

0
 
LVL 14

Expert Comment

by:peetjh
ID: 36473279
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
0
 

Author Comment

by:Seamus2626
ID: 36473343
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
0
 
LVL 14

Accepted Solution

by:
peetjh earned 300 total points
ID: 36473380
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

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 200 total points
ID: 36473404
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

0
 

Author Closing Comment

by:Seamus2626
ID: 36473469
Thanks guys!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question