Solved

Delete row if not contains certain text

Posted on 2016-09-01
7
59 Views
Last Modified: 2016-09-01
I wanted to record this but I don't think Excel can record conditionals, i.e. IF, ELSE, etc. So, I thought this may be easy for an Excel VB expert.

I need a Excel macro that will...
- Search whole worksheet and will delete a row if the Project Title cell...
- does not end with string: / total Total Fee
- and does not start with at least a minimum length 4 character number string. These pass: 2100, 9812
- and those same strings may also be like: 210012, 210012A, 210012A1, 98123D

I've attached a sample spreadsheet.
DelRowIfNotProject.xlsx
0
Comment
Question by:NVIT
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 400 total points
ID: 41780847
Try this macro...

Sub DeleteRows()

    Dim DeleteRange As Range, rw As Long
    Const EndsWith As String = "/ Total Total Fee"
    
    For rw = 2 To Cells(Rows.Count, "A").End(xlUp).Row
        If LCase(Right(Cells(rw, "C"), Len(EndsWith))) <> LCase(EndsWith) And _
            IsNumeric(Replace(Replace(Left(Cells(rw, "C"), 4), " ", "X"), "-", "X")) = False Then
            If DeleteRange Is Nothing Then
                Set DeleteRange = Cells(rw, "A").EntireRow
            Else
                Set DeleteRange = Union(DeleteRange, Cells(rw, "A").EntireRow)
            End If
        End If
    Next
    
    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete
    End If
    
End Sub

Open in new window

0
 
LVL 23

Author Comment

by:NVIT
ID: 41780989
Hi Wayne,

It's not deleting the "001 Billing Group / total Total Fee" lines. Is it a fairly quick fix?
It's deleting the rest, though. Thanks for that.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41780994
According to the rules you posted, it shouldn't delete those lines...

- does not end with string: / total Total Fee
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 100 total points
ID: 41780995
I assume you need to replace And with Or in line#7 in the end.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41780999
I assume you need to replace And with Or in line#7 in the end.

Could be. I read the rules as being 2 separate conditions, not the one.
0
 
LVL 23

Author Closing Comment

by:NVIT
ID: 41781000
@Wayne

I meant it as:
- does not end with string: / total Total Fee
- [b]and[/b] does not start with at least a minimum length 4 character number string. These pass: 2100, 9812

Please forgive me for not being clear.

@Subohd

That's it. It works.

Thank you both. This is very helpful.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41781003
You're welcome. Glad we could help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now