Solved

Delete all rows based on values in a table

Posted on 2012-03-25
3
519 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 41

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 41

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 41

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

762 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

20 Experts available now in Live!

Get 1:1 Help Now