+ Reply to Thread
Results 1 to 9 of 9

Macro to autofilter Columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Macro to autofilter Columns

    I would like a macro to autofilter all the columns and filter the values in the columns with header "bank charges" (will not always be in Col g as new columns are added from time to time)


    I have tried to write code to do this, but it does not filter from row 5 and also get a run time error


    "autofilter method of range class failed"


    .Range("$A$5:$DZ" & LR).Autofilter Field:="Bank Charges", Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>" _

     Sub Autofilter()
    
    Dim LR As Long
    
    Sheets("Data").Select
    LR = Range("A" & Rows.Count).End(xlUp).Row
        With Range("A5")
        .Range(Selection, Selection.End(xlToRight)).Autofilter
        .Range("$A$5:$DZ" & LR).Autofilter Field:="Bank Charges", Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>" _
    
         End With
          
          
          End Sub

    I have also posted on https://www.mrexcel.com/forum/excel-...eo-blanks.html
    Attached Files Attached Files

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Mcro to autofilter Columns

    Hi Howard,

    How about...

    Sub Autofilter()
    
        Dim x As Long
        Dim f As Range
        Set f = Rows("5:5").Find(what:="Bank Charges", LookIn:=xlValues)
        
        x = f.Column
        
        Sheets("Data").Select
        With Range("A5").CurrentRegion
            .Autofilter Field:=x, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>"
        End With
    
    End Sub
    Last edited by jeffreybrown; 04-02-2019 at 09:18 AM.
    HTH
    Regards, Jeff

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Mcro to autofilter Columns

    Thanks Jeff. The code filter the data 100%

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,810

    Re: Mcro to autofilter Columns

    Try:

    With Range("A5")
        .Autofilter
        .Resize(, 8).Autofilter _
            Field:=7, _
            Criteria1:="<>0", Operator:=xlAnd, _
            Criteria2:="<>"
    End With
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Mcro to autofilter Columns

    Thanks for the help Trevor.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Macro to autofilter Columns

    Good to hear Howard and thanks for the feedback. We are happy to help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  7. #7
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Macro to autofilter Columns

    Hi Jeffrey


    I have rechecked my data after running the macro and when I click on the filter all the values are unchecked. I want all the values checked, except zero (0.00) and blank items


    I have attached my sample data


    Kindly check & amend
    Attached Files Attached Files

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Macro to autofilter Columns

    Using the code below and your data, after the filter is applied only three rows are visible.

    Sub Autofilter()
        Dim x As Long
        Set f = Rows("5:5").Find(what:="Bank Charges", LookIn:=xlValues)
        Sheets("Data").Select
        With Range("A5").CurrentRegion
            .Autofilter Field:=f.Column, Criteria1:="<>0", Operator:=xlAnd, Criteria2:="<>"
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Macro to autofilter Columns

    Thanks Jeffrey. Its working 100%

+ 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. Macro to autofilter columns based on a cell value
    By Elainefish in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-06-2019, 09:56 AM
  2. Autofilter VBA Macro to filter out individual columns across entire workbook
    By prnetreba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2017, 06:45 PM
  3. Macro Autofilter same criteria across multiple columns
    By Kungfubarbie in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-07-2014, 04:35 AM
  4. macro to autofilter and copy first 15 lines visible lines in columns D:E
    By Juraj123 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2013, 03:40 AM
  5. Macro copying and pasting "wrong" columns with AutoFilter
    By MattWilson79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2012, 12:33 PM
  6. Macro - AutoFilter Multiple Columns Containing "..."
    By nkersh812 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2010, 12:10 PM
  7. AutoFilter 2 columns
    By mjcowley in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-22-2009, 08:33 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