+ Reply to Thread
Results 1 to 9 of 9

IF cell contains * or * delete entire Row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    IF cell contains * or * delete entire Row

    Trying to assertain why this macro will not work.
    If Not IsError(.Value) works fine with one item
    but If Not IsError(Application.Match(.Value, _ - will not delete the row items from the list in col A

    What am I missing - End If is active with If Not IsError(.Value) but compile error end if w/o block if

    Sub Delete_Row_IfCellContainsCertainText()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
        With ActiveSheet
            .Select
    
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
            .DisplayPageBreaks = False
    
            Firstrow = .UsedRange.Cells(1).Row
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            For Lrow = Lastrow To Firstrow Step -1
                With .Cells(Lrow, "A")
      '              If Not IsError(.Value) Then
      '                  If .Value Like "*CSB*" Then .EntireRow.Delete
                    If Not IsError(Application.Match(.Value, _
                    Array("*CSB *", "*Hanover County Community Svcs Board", "*Valley Community Service Board", "*District Three Transportation", "*MEOC *", "*Appalachian Agency For Senior Citizens, Inc.", "*Behavioral Health Associates", "*Franklin Co. Dept. of Aging", "*New River Valley Senior Services", "*Piedmont Senior Resources *", "*Lake Country AAA Inc.", "*Bay Aging - Bay Transit", "*Senior Services of Southeastern VA", "GAS*", "GR *", "VD *", "* PT *"), 0)) Then .EntireRow.Delete
      '              End If
                End With
            Next Lrow
        End With
        
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: IF cell contains * or * delete entire Row

    Check your indents. You are missing an End If before the End With/Next Lrow/End With
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: IF cell contains * or * delete entire Row

    I get no compile error with your code.
    You have 3 lines commented out. Are you sure the last of those lines was a comment when you got the compile error?

  4. #4
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Re: IF cell contains * or * delete entire Row

    If I uncomment the End If I get the error message

    Can you show me where it's needed so I don't get the error msg?
    Attached Files Attached Files
    Last edited by 3345james; 05-18-2018 at 10:52 AM. Reason: attach a file for testing

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: IF cell contains * or * delete entire Row

    You don't need it. Leave it as a comment.

  6. #6
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Re: IF cell contains * or * delete entire Row

    I attached a file

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: IF cell contains * or * delete entire Row

    How about
    Sub Delete_Row_IfCellContainsCertainText()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
        Dim Ary As Variant
        Dim i As Long
       
       Ary = Array(" * CSB * ", "*Hanover County Community Svcs Board", "*Valley Community Service Board", "*District Three Transportation", "*MEOC *", "*Appalachian Agency For Senior Citizens Inc.", "*Behavioral Health Associates", "*Franklin Co. Dept. of Aging", "*New River Valley Senior Services", "*Piedmont Senior Resources *", "*Lake Country AAA Inc.", "*Bay Aging - Bay Transit", "*Senior Services of Southeastern VA", "GAS*", "GR *", "VD *", "* PT *")
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
        With ActiveSheet
    
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
            .DisplayPageBreaks = False
    
            Firstrow = .UsedRange.Cells(1).Row
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            For Lrow = Lastrow To Firstrow Step -1
                With .Cells(Lrow, "A")
                    For i = 0 To UBound(Ary)
                      If .Value Like Ary(i) Then
                         .EntireRow.Delete
                          Exit For
                       End If
                   Next i
                End With
            Next Lrow
        End With
        
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    End Sub

  8. #8
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Re: IF cell contains * or * delete entire Row

    Fantastic - Thanks!
    Thank you also for placing into an array - helps me know how for future use.
    Appreciated
    jim

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: IF cell contains * or * delete entire Row

    Glad to help & thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] If cell NOT equal to 'this' OR 'that' then delete entire row
    By terratushi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-16-2018, 03:58 PM
  2. VBA code to delete the FILTERED range and moving the cell up (dont Delete entire row)
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2015, 07:48 AM
  3. [SOLVED] Delete Entire Row if cell contains specified value
    By kosti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-05-2015, 07:39 AM
  4. delete entire row if cell value below X
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-18-2013, 04:42 PM
  5. [SOLVED] Delete cell only with certain critria. Don't want to delete entire row, the celsl only
    By RobertOHare in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-16-2013, 04:18 AM
  6. IF cell contains value - delete entire row
    By Chris2010 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-30-2010, 10:44 PM
  7. Delete entire row when cell says delete
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-27-2007, 11:46 AM

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