Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete all rows based on values in a table

Posted on 2012-03-25
3
Medium Priority
?
567 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 2000 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

704 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