Solved

Delete all rows based on values in a table

Posted on 2012-03-25
3
548 Views
Last Modified: 2012-03-26
In Excel 2007, I am using the below Sub to delete all rows that have the word "SODA -" in Column C.  Is there a way that I could use an external excel table for the values.  This would allow adding new values to be deleted without having to go into the Sub.

For example, the table would look like

COLUMN C

SODA -
CANDY -
CHIPS -
COOKIES -

Then the Sub would delete all rows  if the values was in the table.  It would delete "SODA -", "CANDY - ", "CHIPS -", and "COOKIES -".

*******************************************************

Sub Step1_DeleteRowsWithoutSODAinColC()
 ' This macro deletes all rows on the active worksheet
 ' that do not have (SODA-)  in column C.
  Dim rng As Range, cell As Range, del As Range
  Dim strCellValue As String
  Set rng = Intersect(Range("C2:C65000"), ActiveSheet.UsedRange)
  For Each cell In rng

    strCellValue = (cell.Value)
    If InStr(strCellValue, "SODA -") = 0 Then
        If del Is Nothing Then
           Set del = cell
        Else: Set del = Union(del, cell)
        End If
     End If
  Next cell
  On Error Resume Next
  del.EntireRow.Delete
 End Sub
0
Comment
Question by:mato01
  • 3
3 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37763684
There's about a half dozen ways to go at this, you could catenate all the foods together into one string and check, you could add the table to a dictionary and test for .exists, but in alignment with what you already have, we just loop through the table to find a match using InStr function, and if the match is found, we exit that loop (no need for further processing) then allow the outer loop to keep marching along.


Table:  Range name called "Goodies"

Sub Step1_DeleteRowsWithoutSODAinColC()
 ' This macro deletes all rows on the active worksheet
 ' that do not have (SODA-)  in column C.
  Dim rng As Range, cell As Range, del As Range
  Dim strCellValue As String
  Dim myCheck As Range
  
  Set rng = Intersect(Range("C2:C" & Rows.Count), ActiveSheet.UsedRange)
  
  For Each cell In rng.SpecialCells(xlCellTypeConstants)

    strCellValue = (cell.Value)
    For Each myCheck In [Goodies] 'named range of items to delete
        If InStr(strCellValue, myCheck) <> 0 Then '<> 0 means the goodie was found
            If del Is Nothing Then
               Set del = cell
            Else
                Set del = Union(del, cell)
            End If
            Exit For 'found it, so no need to loop, anymore
         End If
    Next myCheck
  Next cell
  
  If Not del Is Nothing Then del.EntireRow.Delete
  
 End Sub

Open in new window


See attached.

Dave
delGoodies-r1.xls
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37763699
PS - I've found that the .UsedRange property is not always accurate, so an alternative that I almost always use to substitute for:

Set rng = Intersect(Range("C2:C" & Rows.Count), ActiveSheet.UsedRange)

Is:

Set rng = Range("C2",Range("C" & Rows.Count).End(xlUp))

This will always ensure the last cell in rng is one that has data, rather than an incorrect UsedRange lower boundary.

Note, also, the UsedRange boundary can also be Excel-defined by formatting and cells that have no data at all!

Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 37763717
Your original question is a bit confusing as you state:

>>I am using the below Sub to delete all rows that have the word "SODA -" in Column C

However, your subroutine is actually documented (and coded correctly) stating:

>> ' This macro deletes all rows on the active worksheet
>> ' that do not have (SODA-)  in column C.

My original post deletes all rows based on column C having items in the table.

This particular post gives you the other option, to delete all rows based on column C NOT having items in the table.

Here's the code for that particular solution:

Sub Step1_DeleteRowsWithoutSODAinColC()
 ' This macro deletes all rows on the active worksheet
 ' that do not have (SODA-)  in column C.
  Dim rng As Range, cell As Range, del As Range
  Dim strCellValue As String
  Dim myCheck As Range
  Dim bFound As Boolean
  
  Set rng = Intersect(Range("C2:C" & Rows.Count), ActiveSheet.UsedRange)
  
  For Each cell In rng.SpecialCells(xlCellTypeConstants)

    strCellValue = (cell.Value)
    For Each myCheck In [Goodies] 'named range of items to preserve
        If InStr(strCellValue, myCheck) <> 0 Then '<> 0 means the goodie was found
            bFound = True
            Exit For
        End If
    Next myCheck
    
    If Not bFound Then 'if the goodie was not found, then ok to delete row
        If del Is Nothing Then
           Set del = cell
        Else
            Set del = Union(del, cell)
        End If
     End If
     
     bFound = False
  Next cell
  
  If Not del Is Nothing Then del.EntireRow.Delete
  
 End Sub

Open in new window



So, hopefully the first post, or this one really gets you the help you need, lol ;)

See attached.

Dave
delRowGoodie-not-found-r2.xls
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

828 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