Solved

Delete row if not contains certain text

Posted on 2016-09-01
7
88 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
[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
  • 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
Technology Partners: 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 31

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 31

Expert Comment

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

707 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