+ Reply to Thread
Results 1 to 9 of 9

Using VBA to add a filter, filter by certain criteria, then delete filtered rows

  1. #1
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Using VBA to add a filter, filter by certain criteria, then delete filtered rows

    Hello! I would really appreciate the help with a VBA problem here.

    I have attached the working file

    There are 2 sheets in the workbook: the data source and the "End Goal" sheet as a reference.

    Using a VBA code, I wish to apply a filter to the data source sheet, filter Column B to only show 'Singapore', and filter Column C to only show 'Folk', 'Indie', 'Rock'.
    Then delete all the filtered out rows.

    I expect to have more than 1 sheet of data, how can we expand the above to apply to every sheet in the workbook (preferably starting from the second sheet).

    Test.xlsm

  2. #2
    Registered User
    Join Date
    12-01-2011
    Location
    India
    MS-Off Ver
    Microsoft 365
    Posts
    33

    Lightbulb Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows

    I Suggest you go for Power Query option for this requirement instead of VBA. These are inbuilt function with higher reliability and flexibility.

    M Code (added by Moderator):

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by AliGW; 07-10-2023 at 11:57 AM. Reason: Code added.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Post Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows


    ( removed )
    Last edited by Marc L; 07-10-2023 at 11:37 AM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this !


    A ThisWorkbook module Excel basics VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
        
    With Sheets(1).[AF2]
            .
    Formula "=(B2<>""Singapore"")+((C2<>""Folk"")*(C2<>""Indie"")*(C2<>""Rock""))>0"
        
    For S& = 2 To Sheets.Count
        With Sheets
    (S).[A1].CurrentRegion
            
    .AdvancedFilter 1Sheets(1).[AF1:AF2]
             If 
    Application.Subtotal(103, .Columns(1)) > 1 Then .Offset(1).Rows.Delete xlShiftUp
            
    .Parent.ShowAllData
        End With
        Next
            
    .Clear
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows

    If I have more than 1 data source sheet to comb through and still want to use the same filters for every sheet, how can loop through all the sheets with the same code?

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows


    Just see post #4 at least !

  7. #7
    Registered User
    Join Date
    05-01-2023
    Location
    Singapore
    MS-Off Ver
    (Version 2302 Build 16.0.16130.20378) 64-bit
    Posts
    66

    Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows

    Hi, it doesn't work.

    S& is not defined. And .Formula line has error too.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,555

    Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows

    try
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Using VBA to add a filter, filter by certain criteria, then delete filtered rows


    Quote Originally Posted by webcam723 View Post
    Hi, it doesn't work.
    As it well works on my side …

+ 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] Filter Column > Select All Filtered Rows exc. Header > Delete Rows
    By jbransden in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-12-2021, 08:09 AM
  2. Filter table by multiple criteria and delete all irrelevant rows
    By ITY in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2020, 06:28 AM
  3. Select and Delete rows without using filter using multiple criteria
    By sunil10007 in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 11-02-2019, 06:21 AM
  4. Replies: 6
    Last Post: 10-16-2014, 08:42 AM
  5. Delete Rows hidden by either Auto Filter and Advanced Filter
    By lesoies in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 10:29 AM
  6. delete rows based on a filter criteria - VBA
    By acs013 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2009, 11:51 PM
  7. Auto Filter Delete Rows by Criteria Doesn't Work Range To Complicated
    By robertjtucker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2005, 01:05 PM

Tags for this Thread

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