Link to home
Start Free TrialLog in
Avatar of mato01
mato01Flag for United States of America

asked on

Delete all rows based on values in a table

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
Avatar of dlmille
dlmille
Flag of United States of America image

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
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
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial