+ Reply to Thread
Results 1 to 6 of 6

Check if filter applied to column - VBA

  1. #1
    Registered User
    Join Date
    03-12-2012
    Location
    London
    MS-Off Ver
    Office 365 ProPlus 64 bit
    Posts
    35

    Check if filter applied to column - VBA

    Hi,

    I was looking for some VBA help if possible! I'm a complete beginner to VBA, but I've researched the web & I've found a piece of VBA that does a lot of what I'm hoping for - but I can't find a way to edit it to be 100% what I need.

    Background: My workbook has a data table (not pivot table) which is being filtered by potentially multiple slicers that the user controls. I need to know which columns are being filtered (i.e. which slicers are currently in use) as there could be multiple at once.

    The below code is doing a good job of telling me which column numbers are currently filtered, but there's 3 problems;



    1. The VBA is launched by running a macro. I would prefer it if the VBA ran automatically any time a slicer is used, I would assume setting the VBA to run on user click might work (I understand from reading it might slow the workbook down, but I'm willing to try it).

    2. The VBA only currently works when the users cursor is placed in the data table itself (named: Query1) otherwise it errors out with "Object Variable or With block variable not set" but in order for point 1 above to work, I guess I need to set the "With" to work with Query 1, I've also seen ListObject mentioned - but I'm not sure how.

    3. The VBA currently returns me my column number in a msg box, but I'm hoping for it to not show a msg box, but instead set the cell value in row 5 of the relevant column to "Yes" if that filter is currently in use. i.e. if the msg box pop up's to say "12" currently it tells me that there's a filter in place on column L, instead of a msgbox, I would like it to set range ("L5") to "Yes", then loop through the rest of the filters. If the msg box would then pop up again to say "13", I would like it instead to set range ("M5") to "Yes".

    I can provide a sample workbook if easier? Can anyone help me with any/all of the above points? Many thanks!!


    CURRENT VBA

    Sub GetColumns()
    Dim Sht As Worksheet
    Dim i As Long

    Set Sht = ActiveSheet

    With Sht.AutoFilter
    For i = 1 To .Filters.Count
    If .Filters(i).On Then
    MsgBox .Range(1, i).Column
    End If
    Next i
    End With
    End Sub
    Last edited by sharper1989; 08-12-2020 at 10:39 AM.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Check if filter applied to column - VBA

    Basically:

    Please Login or Register  to view this content.
    You could call this from a Worksheet_Calculate event assuming you have at least one formula on the sheet that reacts to the filtering.
    Rory

  3. #3
    Registered User
    Join Date
    03-12-2012
    Location
    London
    MS-Off Ver
    Office 365 ProPlus 64 bit
    Posts
    35

    Re: Check if filter applied to column - VBA

    Thank you! That's great! & I did manage to figure out how to set it off from the Worksheet_Calculate event as I do have a formula which reacts to the filtering.

    It is taking a little while to run through all of the different columns in the List Object though as it is a wide table.

    It's actually only columns L, M or N that could have a filter on them - can I adjust the VBA above so it only loops through those columns to maybe speed it up?

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Check if filter applied to column - VBA

    If your table starts in column A, then you could replace this:

    Please Login or Register  to view this content.
    with this:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-12-2012
    Location
    London
    MS-Off Ver
    Office 365 ProPlus 64 bit
    Posts
    35

    Re: Check if filter applied to column - VBA

    You're a star! So much quicker that way. Marking as solved - thank you!

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Check if filter applied to column - VBA

    Glad we could help.

+ 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. Open another workbook, but with filter applied on column
    By dalpai in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2020, 08:03 AM
  2. Column header to fill with a colour when filter applied?
    By JRC1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-03-2017, 09:11 AM
  3. [SOLVED] Filter Column B but check...
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2015, 02:23 PM
  4. Replies: 0
    Last Post: 10-10-2014, 02:02 PM
  5. [SOLVED] Need Macro to Select All Files in a Column After Filter Applied
    By Gard5096 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-12-2012, 05:33 PM
  6. Replies: 5
    Last Post: 05-23-2012, 10:33 AM
  7. [SOLVED] how do i identify first and last row once filter applied
    By pete the greek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-21-2006, 12:25 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