+ Reply to Thread
Results 1 to 6 of 6

vba code to delete rows with string of text found

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2011
    Location
    NYC
    MS-Off Ver
    Excel 2016
    Posts
    36

    vba code to delete rows with string of text found

    i have a large spreadsheet with certain rows i need to delete if they have certain strings of text. i need coding to delete rows only if they contain any of the various certain strings of text as a whole. i found code that deletes rows if any single word is found, but i need it to find only exact words together (e.g. county of residence, - not just if any of those words are found elsewhere like when "of" is found in "zip code of residence"

    so: i need to delete rows that contain "county or residence" or "age of respondent" or "zip code of employment"

    I also need it to search 2 different columns rather than just one.

    (i think macros would be the easiest solution??)
    any help would be appreciated! thanks!

  2. #2
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: vba code to delete rows with string of text found

    Maybe something like this?
    Option Compare Text
    Sub DeleteRowsMatchingText()
        Dim rFoundText  As Range
        Dim rLookRange  As Range
        Dim vText()     As Variant
        Dim vItem       As Variant
        Dim lCalc       As Long
        
        With Application
            lCalc = .Calculation
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        vText = Array("County of Residence", "Age of Respondent", "Zip Code of Employment")
        Set rLookRange = Range("A:A,C:C") 'Change A:A and C:C to your search columns
        
        For Each vItem In vText
            Do
                Set rFoundText = rLookRange.Find(vItem, LookIn:=xlValues, lookat:=xlWhole)
                If Not rFoundText Is Nothing Then
                    rFoundText.EntireRow.Delete
                End If
            Loop Until rFoundText Is Nothing
        Next
        
        Set rLookRange = Nothing
        Set rFoundText = Nothing
        
        With Application
            .ScreenUpdating = True
            .Calculation = lCalc
        End With
    End Sub
    Last edited by gjlindn; 09-14-2011 at 12:30 AM. Reason: typo
    -Greg If this is helpful, pls click Star icon in lower left corner

  3. #3
    Registered User
    Join Date
    09-13-2011
    Location
    NYC
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: vba code to delete rows with string of text found

    works! thanks!!

  4. #4
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: vba code to delete rows with string of text found

    How about this?
    Option Compare Text
    Option Explicit
    
    Sub DeleteRowsMatchingText()
        Dim rFoundText  As Range
        Dim rLookRange  As Range
        Dim vText()     As Variant
        Dim vItem       As Variant
        Dim lCalc       As Long
        
        With Application
            lCalc = .Calculation
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        vText = Array("County of Residence", "Age of Respondent", "Zip Code of Employment")
        Set rLookRange = Range("A:A,C:C") 'Change A:A and C:C to your search columns
        
        For Each vItem In vText
            Do
                Set rFoundText = rLookRange.Find(vItem, LookIn:=xlValues, lookat:=xlWhole)
                If Not rFoundText Is Nothing Then
                    rFoundText.EntireRow.Delete
                End If
            Loop Until rFoundText Is Nothing
        Next
        
        Set rLookRange = Nothing
        Set rFoundText = Nothing
        
        With Application
            .ScreenUpdating = True
            .Calculation = lCalc
        End With
    End Sub

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: vba code to delete rows with string of text found

    If you need to do this an a regular bases then a macor is a good way to go - It also depends on how many rows need to be checked.

    It may alo possible to use formulas, manual sorting and manula deleting

    The early part of thread
    HTML Code: 
    has various methods of using a macro to search for a whole cell content in a single column
    These various methods can be modified to search a 2nd column.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  6. #6
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: vba code to delete rows with string of text found

    Glad I could help If you haven't already, you should also check out the link mudraker posted. It's great to be able to use vba to solve an issue, but with a little bit of formula and native Excel function knowledge you can decide for yourself if vba is appropriate. Have a great day!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1