Consulting

Results 1 to 4 of 4

Thread: If filtered rows are Yellow then delete else ignore and remove filter

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location

    If filtered rows are Yellow then delete else ignore and remove filter

    Hello Everybody,

    I have a macro which is filtering data on Yellow color and deleting the rows... but the problem is that if there are no yellow rows, code is deleting all the rows available.

    Can anybody suggest a solution that if after filter yellow rows are available then delete else remove filter.

    Code I have:

    ActiveSheet.UsedRange.AutoFilter Field:=8, Criteria1:=RGB(255, _
    255, 0), Operator:=xlFilterCellColor

    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    Application.DisplayAlerts = True


    ActiveSheet.ShowAllData

    Thanks

    Regards,
    Shan

  2. #2
    VBAX Regular
    Joined
    Nov 2011
    Location
    Ufa
    Posts
    75
    Location
    try this
    With ActiveSheet
        .UsedRange.AutoFilter Field:=8, Criteria1:=RGB(255, 255, 0), Operator:=xlFilterCellColor
        If .AutoFilter.Range.Columns(1).SpecialCells(12).Count > 1 Then _
           .UsedRange.Offset(1, 0).Resize(.UsedRange.Rows.Count - 1).EntireRow.Delete
        .ShowAllData
    End With

  3. #3
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location
    Thank you for your reply. Code is working fine! Gr8 !!

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    very similar to nilem's code.
    but you don't need to test if there are rows that meet the autofilter criteria as it deletes the visible (excludes row 1 via offset function) rows only..

    Sub vbax_54413_Delete_AutoFilter_Rows()
        With ActiveSheet
             .AutoFilterMode = False
             .Cells(1).AutoFilter Field:=8, Criteria1:=RGB(255, 255, 0), Operator:=xlFilterCellColor
             .UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete
             .AutoFilterMode = False
         End With
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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