+ Reply to Thread
Results 1 to 5 of 5

Macro to filter 2 columns and then delete remaining rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-30-2006
    MS-Off Ver
    O365
    Posts
    311

    Macro to filter 2 columns and then delete remaining rows

    Hi everyone,
    I'm hoping someone might be able to help me.

    Column C has a possible text of "Enrolled", "Failed" or "Discontinued"
    Column E has possible text including:
    "Visit 1 (Week 1)"
    "Visit 1 (week 2)" etc
    "Visit 2 (week 1)"
    "Visit 2 (Week 2)" etc
    all the way up to "Visit 22 (Week 52)" as well as other text which is not related to my question such as "Early Termination".
    I need to build a macro to filter column C for "Enrolled" and then filter column E for everything that starts with "Visit 7" up to "Visit 22". I then need to delete all those remaining rows (Enrolled Visit 7+) and then remove the filters to show everything remaining.

    I hope this makes sense to someone! Feel free to let me know if you need any more information.

    Many thanks
    Stuart

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,288

    Re: Macro to filter 2 columns and then delete remaining rows

    This assumes headers in Row 1, and that you want to delete "Enrolled" and "Week 7+" not the other way around. Try it on a copy of your worksheet first...

    Sub TestMacro()
        Dim lngR As Long
        Set lngR = Cells(Rows.Count, "C").End(xlUp).Row
        
        Range("F:F").Insert
        
        With Range("F2:F" & lngR)
            .FormulaR1C1 = "=VALUE(MID(RC[-1],6,3))>=7"
            .Value = .Value
        End With
        
        With Columns("C:F")
            .AutoFilter Field:=1, Criteria1:="Enrolled"
            .AutoFilter Field:=4, Criteria1:="TRUE"
        End With
        
        Range("C2:C" & lngR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        Cells.AutoFilter
        Range("F:F").Delete
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    10-30-2006
    MS-Off Ver
    O365
    Posts
    311

    Re: Macro to filter 2 columns and then delete remaining rows

    Thanks for the reply Bernie,
    I actually wanted to apply both filters and then delete what was remaining so I don't think this would work (although i've not tested it). I've managed to record a macro now anyway, with a lot of cell manipulation - you could say the long way around but it does what i need it to do so i'm happy with that. I don't think it's worth me posting my result because i'm sure there is a much better way of doing it and it would take me an hour to type each step!

    Thanks for your continues help everyone

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,288

    Re: Macro to filter 2 columns and then delete remaining rows

    apply both filters and then delete what was remaining
    That is exactly what the code does.

  5. #5
    Forum Contributor
    Join Date
    10-30-2006
    MS-Off Ver
    O365
    Posts
    311

    Re: Macro to filter 2 columns and then delete remaining rows

    Ah, then that's much easier than my way
    Thanks for your help anyway

+ 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] Want to Delete Specified Columns and then add Column header to the remaining ones
    By ppilot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-16-2014, 12:41 PM
  2. Delete & Merge Columns,Delete Rows with filter, etc
    By traderindia in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2009, 02:12 AM
  3. [SOLVED] Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  4. [SOLVED] Re: Macro to delete sheets and saves remaining file does not properly delete module
    By bhawane in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  5. [SOLVED] Re: Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 PM

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