mato01
asked on
Delete rows if value in column does not contain certain criteria
I need to delete the entire row if the value in Column C does not contain "Yellow (Y)", "Blue (B)", or "Orange (O)".
ASKER
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
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
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:
Attached.
PS - if you're still having difficulties, post a sample.
Dave
checkColors-r2.xls
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
Attached.
PS - if you're still having difficulties, post a sample.
Dave
checkColors-r2.xls
ASKER
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
Anyway, it isn't quite working for me. I've attached a sample file with data and code.
Test-Pens.xlsm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect. Thanks
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
Cheers,
Dave
Open in new window
See attached demonstration workbook.
Dave
checkColors-r1.xls