Solved

Delete row if not contains certain text

Posted on 2016-09-01
7
73 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 24

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 30

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 24

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 30

Expert Comment

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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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