+ Reply to Thread
Results 1 to 6 of 6

Delete rows that match criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2008
    Location
    Boston
    MS-Off Ver
    2003 SP3
    Posts
    42

    Delete rows that match criteria

    I would like a macro that scrolls through each row and deletes it if it contains the word "Account" in column B.

    Thanks for any help
    Last edited by PhilSM; 02-02-2009 at 11:07 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Delete rows that match criteria

    There a variety of ways to do this ... some will loop but I quite like to use the following method:

    Public Sub RemoveAccount()
    With Range(Cells(1, "B"), Cells(Rows.Count, "B").End(xlUp)).Offset(0, Columns.Count - 2)
        .FormulaR1C1 = "=IF(ISNUMBER(SEARCH(""Account"",RC2)),1,""x"")"
        .SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Delete
        .Clear
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    11-06-2008
    Location
    Boston
    MS-Off Ver
    2003 SP3
    Posts
    42

    Re: Delete rows that match criteria

    Quite like this, is it possible to make the search and column variable ?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Delete rows that match criteria

    To be honest if you wanted it to be dynamic etc you would be best served reverting to the traditional loop & find method ... such that you can easily do partial matches etc...

    A slightly more dynamic version of the prior method would be along the lines of:

    Public Sub RemoveX()
    Dim strSearch As String, rngCol As Range, bErr As Byte
    Rem Determine Criteria & Range
    strSearch = Application.InputBox("Enter Search String", "Find", Type:=2)
    On Error Resume Next
    Set rngCol = Application.InputBox("Select Column", "Where", Type:=8)
    On Error GoTo 0
    Rem Validate
    If strSearch = "" Then bErr = 1
    If rngCol Is Nothing Then bErr = 2
    If bErr = 0 Then bErr = 2 * Abs(rngCol.Columns.Count > 1)
    If bErr > 0 Then
        Select Case bErr
            Case 1
                strErrMsg = "Invalid Search String (min 1 Char)"
            Case 2
                strErrMsg = "Invalid Range Selection (max 1 Column)"
        End Select
        MsgBox strErrMsg & vbLf & vbLf & "Routine Terminated", vbCritical, "Fatal Error"
    Else
        With Range(Cells(1, rngCol.Column), Cells(Rows.Count, rngCol.Column).End(xlUp)).Offset(0, Columns.Count - rngCol.Column)
            .FormulaR1C1 = "=IF(ISNUMBER(SEARCH(""" & strSearch & """,RC" & rngCol.Column & ")),1,""x"")"
            On Error Resume Next
            .SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Delete
            On Error GoTo 0
            .Clear
        End With
    End If
    Set rngCol = Nothing
    End Sub
    But to reiterate - making the above more flexible is pretty worthless given standard Find method offers greater inherent flexibility for less effort ;-)

    As I say for the odd check here & there I prefer the .SpecialCells method as I'm not a fan of looping... anything more complex... Find & Loop... you should find lots of examples here of the Find & Delete approach.
    Last edited by DonkeyOte; 02-02-2009 at 08:55 AM. Reason: typo's...

  5. #5
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: Delete rows that match criteria

    try this if this is of any help


    Below code will ask you what you want to delete and in which column u want to search

    Eg. Input what u want to delete as "Account"
    and for Column enter 1 , 2 , 3 etc.

    Sub del_macro()
        Dim key As String
        Dim col As Integer
        
        
        key = InputBox("Enter the string to search and delete")
        col = InputBox("Enter the coloum to search the string for deleting")
        
        Dim rTable As Range
        Cells(1, 1).Select
        Set rTable = Selection.CurrentRegion
        Selection.AutoFilter field:=col, Criteria1:=key
        
        rTable.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        ActiveSheet.AutoFilterMode = False
        last_row = ActiveSheet.UsedRange.Rows.Count
    
        
    End Sub

  6. #6
    Registered User
    Join Date
    11-06-2008
    Location
    Boston
    MS-Off Ver
    2003 SP3
    Posts
    42

    Re: Delete rows that match criteria

    Thanks very much, these both work a treat.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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