Results 1 to 3 of 3

Thread: Deleting certain rows using VBA

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    150

    Deleting certain rows using VBA

    Hi.

    I was looking for some assistance please, I have spent the best part of today trying to find a way to do this but really struggling

    Whilst browsing online I found a few examples but when I tried using these none of them have worked for me

    I have a workbook with several worksheets, on each worksheet I need to delete all rows that meet the following criteria

    1, All cells in columns B that are blank, and
    2, All cells in column B that contain text "Blue" or "Black"

    As far as deleting all rows that are blank I have this working using the following coding

    Code:
    If wst.Name Like "Open" = False Then
        On Error Resume Next
        wst.Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        End If
        
        Next wst
    But i am not sure why the following does not work? it does remove all the blank rows but doesnt delete the rows where column b contains blue or black

    Code:
    If wst.Name Like "Open" = False Then
        On Error Resume Next
        wst.Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        End If
        
    With ActiveSheet
        .AutoFilterMode = False
        With Range("b1", Range("b" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*Blue*" or "*Black*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
    
    Next wst
    Any help is appreciated, thanks

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Deleting certain rows using VBA

    you would need to autofilter twice, once for blue and once for black
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Deleting certain rows using VBA

    For multiple filter criteria, something like:

    Code:
    Sub afilter()
        Dim ws As Worksheet
        Dim rng As Range
        
        Set ws = ActiveSheet
        Set rng = ws.Range("a1:a9")   'change to your range
        
        With rng
            .AutoFilter field:=1, Criteria1:="=black", Operator:=xlOr, Criteria2:="=blue"
            
        End With
    
    End Sub
    My example is not using wildcards, of course. Would need to change to accommodate.

Posting Permissions

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



Click Here to Expand Forum to Full Width