Solved

Delete rows if value in column does not contain certain criteria

Posted on 2012-03-17
7
218 Views
Last Modified: 2012-03-17
I need to delete the entire row if the value in  Column C does not contain "Yellow (Y)", "Blue (B)", or "Orange (O)".
0
Comment
Question by:mato01
  • 4
  • 3
7 Comments
 
LVL 41

Expert Comment

by:dlmille
ID: 37733370
As requested

Sub delRowsCriteria()
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim r As Range
Dim lastRow As Long
Dim rDelete As Range
Dim sCheck As String

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    lastRow = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set rng = wks.Range("C1:C" & lastRow)
    
    For Each r In rng
        sCheck = Application.WorksheetFunction.Trim(r.Value)
    
        If UCase(sCheck) <> "YELLOW (Y)" And UCase(sCheck) <> "BLUE (B)" And UCase(sCheck) <> "ORANGE (O)" Then
            If rDelete Is Nothing Then
                Set rDelete = r
            Else
                Set rDelete = Union(r, rDelete)
            End If
        End If
    Next r
    
    rDelete.EntireRow.Delete
        
End Sub

Open in new window


See attached demonstration workbook.

Dave
checkColors-r1.xls
0
 

Author Comment

by:mato01
ID: 37733425
I was using the colors generically.  When I put in the actual text, it deletes all the rows instead of leaving me with

Mexico (MEX)
Canada (CAN)
U.S.A., P&US (UPU)
Europe-S.America (ESA)

For example, I put in the real data in your sample, and in the real data, and it did not work. Is there something else in the code I need to change. except for  this line.

 If UCase(sCheck) <> "Mexico (MEX)" And UCase(sCheck) <> "Canada (CAN)" And UCase(sCheck) <> "U.S.A., P&US (UPU)"  And UCase(sCheck) <> "Europe-S.America (ESA)Then
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37733432
You did not specify what your criteria would be other than what you specified in your original question.  You have to be more specific as we cannot read minds - we're getting close, but not there yet ;)

Yes, but the line would need to be:

 If UCase(sCheck) <> "MEXICO (MEX)" And UCase(sCheck) <> "CANADA (CAN)" And UCase(sCheck) <> "U.S.A., P&US (UPU)"  And UCase(sCheck) <> "EUROPE-S.AMERICA (ESA)" Then

The UCASE converts all to upper case so this is without case sensitivity.  If you want case sensitivity, just take the UCASE function out.

Here's another way using a case statement, so a bit cleaner if you have a lot of criteria:
Option Explicit

Sub delRowsCriteria()
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim r As Range
Dim lastRow As Long
Dim rDelete As Range
Dim sCheck As String

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    lastRow = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set rng = wks.Range("C1:C" & lastRow)
    
    For Each r In rng
        sCheck = Application.WorksheetFunction.Trim(r.Value)
    
        Select Case UCase(sCheck):
            Case "YELLOW (Y)", "BLUE (B)", "ORANGE (O)":  'add as many as you need
                'do nothing
            Case Else:
        
                If rDelete Is Nothing Then
                    Set rDelete = r
                Else
                    Set rDelete = Union(r, rDelete)
                End If
        End Select
        
    Next r
    
    rDelete.EntireRow.Delete
        
End Sub

Open in new window


Attached.

PS - if you're still having difficulties, post a sample.


Dave
checkColors-r2.xls
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.

 

Author Comment

by:mato01
ID: 37733526
Okay. Sorry for the confusion. I was just trying to be careful on what information I posted.

Anyway, it isn't quite working for me.  I've attached a sample file with data and code.
Test-Pens.xlsm
0
 
LVL 41

Accepted Solution

by:
dlmille earned 200 total points
ID: 37733530
Thanks for sending a sample.  First, we were using UCASE as to not be case sensitive, but your criteria is in upper/lower case.  So, I took the UCASE out and checked all spelling.

Works like a charm, now.

Let me know if you have further difficulties.

See attached.

Dave
Test-Pens-r1.xlsm
0
 

Author Closing Comment

by:mato01
ID: 37733604
Perfect. Thanks
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37733620
You know you have unlimited points to distribute.  I pick up any question, but other experts may pass you buy with low point totals, as they think perhaps you don't think its that important/urgent and move on to the more urgent ones.

Cheers,

Dave
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

Title # Comments Views Activity
macro for finding text and replacing 7 43
Macro 6 49
Display the VBA Userform in Screen Center 4 31
Moving Excel to AaaS 4 36
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

914 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

17 Experts available now in Live!

Get 1:1 Help Now