+ Reply to Thread
Results 1 to 7 of 7

VBA Autofilter

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-04-2016
    Location
    Newcastle
    MS-Off Ver
    2016
    Posts
    107

    Lightbulb VBA Autofilter

    I have a spreadsheet in which i want to display my information by two conditions.

    In column 1 i only want it to filter to "Y"; HOWEVER if column 2 contains ANY text, i want the filter to be ignored and it to display the entire row.

    Here's some images of an example sheet below:

    1.png
    2.png

    Thanks,
    Liam
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Autofilter

    Quote Originally Posted by liamfrancis2013 View Post
    In column 1 i only want it to filter to "Y"; HOWEVER if column 2 contains ANY text, i want the filter to be ignored and it to display the entire row.
    Can you explain better that single part, what I understood was:
    Filter column A with equal "Y" and then ? only show the line that has text on coumn B ?

  3. #3
    Forum Contributor
    Join Date
    01-04-2016
    Location
    Newcastle
    MS-Off Ver
    2016
    Posts
    107

    Re: VBA Autofilter

    Basically when i filter Column A to "Y" it will only show the cells that contain "Y", as you'd expect. However i also want any cell that has a value in it in column B to not be effected by the filter in column A.

    With the aim of making my sheet look something like this:
    1.png

  4. #4
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Autofilter

    Well, it's weird I'm not finding anything but I'm not sure if it's possible using the filters because I can't find a way to do it manually (without VBA) so maybe using some loops.
    I'm gonna see if I get anything. You need something like:
    .Range("A1:B1").AutoFilter Field:=1, Criteria1:=("Y"), xlOr, Field:=2, Criteria1:=("*")
    the problem is that the xlOr only seems to work with different criterias, not different columns/fields...
    Last edited by rdrmdr; 05-11-2016 at 06:15 AM.

  5. #5
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Autofilter

    The only thing I got is this:
    Sub Button1_Click()
        For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
            If Cells(i, 1) <> "Y" And Cells(i, 2) = "" Then Rows(i & ":" & i).EntireRow.Hidden = True
        Next i
    End Sub
    It works, but you will have to work with it for different situations. Cells(Rows.Count, "A").End(xlUp).Row is to get the last cell on column A with something written (in this case, 11)...

  6. #6
    Forum Contributor
    Join Date
    01-04-2016
    Location
    Newcastle
    MS-Off Ver
    2016
    Posts
    107

    Re: VBA Autofilter

    Thanks, this looks like it might be the solution i'm after, i'm going to attempt applying it to the real version of my sheet now

  7. #7
    Registered User
    Join Date
    04-29-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Autofilter

    Just some things to note when applying this on a different project:
    -i starts with 2 so if you don't change it, you can't have the table lower to for example, starting at A5;
    -the loop counts the last cell on column A if you don't change, you can't have the table in, for example, C1;
    -that makes it so you can't have anything on column A after the table or the loop will do untill that cell with something that doesn't belong to the table;
    -don't forget to change the <> "Y" and the = "";
    -don't forget to change the 1 and the 2 inside the Cells() on the If statment;
    -This hides the row, the same as the filter I think but it doesn't apply any filter so to make it appear, you do EntireRow.Hidden = False or clean your filters if you have any...

+ 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] Copy top cell post autofilter and re-apply autofilter based on cell value
    By gpato in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2014, 09:07 PM
  2. Replies: 4
    Last Post: 03-16-2013, 08:33 AM
  3. Replies: 2
    Last Post: 07-06-2012, 11:42 AM
  4. in vba how to turn off autofilter also how to check if autofilter is on
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-31-2011, 10:04 AM
  5. AutoFilter method of Range class failed - Yet autofilter works.
    By Carlsbergen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2009, 05:43 PM
  6. Capturing the Excel AutoFilter Sorting Event-sort and autofilter options
    By Kognyto in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2008, 05:36 PM
  7. [SOLVED] How to Sort within AutoFilter with Protection on (and AutoFilter .
    By giblon in forum Excel General
    Replies: 1
    Last Post: 02-16-2006, 08:25 AM

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