Solved

Delete all rows based on values in a table

Posted on 2012-03-25
3
559 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
[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
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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

752 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