+ Reply to Thread
Results 1 to 22 of 22

how can I create a macro which will check the selected criteria in filter?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Question how can I create a macro which will check the selected criteria in filter?

    I want to create a macro which will check selected criteria in filter, how would i do that?

    can any one provide me an example?

    Thanks,
    Anand

  2. #2
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    Hi Anand. Try this:
    Private Sub Workbook_Open()
    Application.Calculation = xlAutomatic                   '// enable auto-calculate
    Sheets("Sheet1").Protect UserInterfaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
    End Sub
    Function FilterCriteria(iColms As Integer, iHead As Integer) As String
    
    Dim cl As String, sCri As String, sAry As String   ' sets filter info for filter criteria bar
    Dim arg As Variant                                 ' iColms is number of sort columns in table
    Dim i As Integer                                   ' iHead is table header row
    
    Application.Volatile True
    On Error Resume Next
    For i = 1 To iColms
        With ActiveSheet.AutoFilter.Filters(i)
            If .On Then
                sCri = IIf(Left(.Criteria1, 1) = "=", Right(.Criteria1, Len(.Criteria1) - 1), .Criteria1)
                If .Operator = xlAnd Then
                    sCri = sCri & "  and  " & IIf(Left(.Criteria2, 1) = "=", Right(.Criteria2, Len(.Criteria2) - 1), .Criteria2)
                ElseIf .Operator = xlOr Then
                    sCri = sCri & ", " & IIf(Left(.Criteria2, 1) = "=", Right(.Criteria2, Len(.Criteria2) - 1), .Criteria2)
                End If
                If Err.Number = 0 Then
                    cl = cl & Cells(iHead, i + 2) & " : " & sCri & "        "
                Else
                    sAry = ""
                    For Each arg In .Criteria1
                        sAry = sAry & ", " & Right(arg, Len(arg) - 1)
                    Next arg
                    cl = cl & Cells(iHead, i + 2) & " : " & Right(sAry, Len(sAry) - 1) & "        "
                    Err.Clear
                End If
            End If
        End With
    Next i
    With ActiveSheet.ClearFilter
        If cl = "" Then                                ' hide Clear Filter button
            FilterCriteria = "": .Visible = False
        Else                                           ' show Clear Filter button
            FilterCriteria = cl: .Visible = True
        End If
    End With
    End Function
    Put this into a cell : FilterCriteria(# of columns in your table, Table Header row)

    Where (# of columns in your table) is how many columns you have in your table.
    And (Table Header row) is the row number that the table header is in.


    Now as you change the filter in your table, the filter criteria will appear in the cell with the FilterCriteria() function.

    Have a button on your page with the name "ClearFilter". Attach this code to the button :
    ActiveSheet.ShowAllData
    Pressing the button will clear the filter from your table.
    Last edited by jimmalk; 11-04-2012 at 11:26 PM.

  3. #3
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    Thanks Jim, this code works partially.

    It does not work in two scenario,

    Scenario 1: When we select all, function does not show any output

    Scenario 2: when first criteria of filter is not selected and if we select 2nd and 3rd criteria, then function does not show any output.

    Any suggestion?

  4. #4
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    Scenerio 1 : Yes, it is correct that the cell will show nothing. There is no filter to show. If you would like to show something you could replace FilterCriteria = "" with FilterCriteria = "Table Not Filtered"

    Scenerio 2 : I'm not sure. I use the dropdown buttons to select the filter one at a time. I suppose you are applying a filter to several criteria at one time. I have to look into that.. give me a few minutes

  5. #5
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    Look over the attached file. In the Sub Workbook_Open(), comment out the line "Application.Calculation = xlManual" or set it to xlAutomatic... Should work like you want now. It has an additional capability to right click on any cell to apply a filter. This code does work with multiple criteria, maybe you need to adapt it a little to work with your table? But it should work. Please post back if not.
    Attached Files Attached Files
    Last edited by jimmalk; 11-04-2012 at 11:53 PM.

  6. #6
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    let me try this and I will post if it works or not, Thanks!

  7. #7
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    It looks like I am missing something here, in above spreadsheet, I have unlocked it and wrote a function "=FilterCriteria(5,5)" in cell Q5, and it does not show any value of filter criteria even if I have one criteria selected. any suggestion?

  8. #8
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    You should put into the Q5 (or any other cell you want) "=FilterCriteria(8,5)" ... because there are 8 columns in the table, and the table header is in row 5

    Remember also ... in the Sub Workbook_Open(), comment out the line "Application.Calculation = xlManual" or set it to xlAutomatic
    Last edited by jimmalk; 11-05-2012 at 12:09 AM.

  9. #9
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    Still it does not show anything, I only have one filter criteria selected which is one animal name.

  10. #10
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    check the attached spreadsheet, it still does not show any thing in cell "Q5".
    Attached Files Attached Files

  11. #11
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    2 problems you're having. First, you have the file saved with the worksheet calculation set to manual, it must be set to automatic. In the Sub Workbook_Open() put in this line :
    Application.Calculation = xlAutomatic
    Second problem is that Q5 is on the right and maybe you can't see it. Go into Excel Options and activate the scrollbars, then you can see Q5. I have "=FilterCriteria(8,5)" above the table in cell B2
    Last edited by jimmalk; 11-05-2012 at 01:11 AM.

  12. #12
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    Thank you, I think I will spend some time on this and let you know by tomorrow if I have any more questions, Thanks!

  13. #13
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    You're welcome Anand. I'll watch the post. Good luck

  14. #14
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Question Re: how can I create a macro which will check the selected criteria in filter?

    Here is the file that includes worksheet calculation to Automatic still cell "B2" does not update if I choose some filter criteria in column name "Animal name".

    Any suggestion on what am I missing?

    Thanks!
    Attached Files Attached Files

  15. #15
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    Hi Anand. The file you posted is fine, should work. The only thing I can think of is that you dont have macros enabled. Make sure you enable macros so the vba code can run

  16. #16
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    well the problem is cell "B2" shows value when I right click on any cell inside table but instead if I choose filter criteria manually then it does not update the cell "B2". I checked and Macro is enabled .


  17. #17
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    The last file you posted is working fine. The only way that B2 would not update would be if you placed the calculation mode back into manual. Look in the "Formulas" tab, make sure that "Calculation Options" is set to "Automatic". Should work.

  18. #18
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    I did check it and "Calculation Options" is set to "Automatic". any other ideas?

  19. #19
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    The last file you re-posted works fine. Maybe you unproected the file, and then re-protected it without selecting "Sort" and "Use Autofilter"? The "Sort" and "Use Autofilter" must be checked to use the built-in table filter controls. Other than having "Calculation Options" set to "Automatic" there really cant be anything wrong. Check again the file you last posted and try using that file, maybe by accident you made some change to the vba code and thats why it isnt working now.

  20. #20
    Registered User
    Join Date
    10-20-2012
    Location
    Houston, TX USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: how can I create a macro which will check the selected criteria in filter?

    Jim, you are right, I looked into that file again and it works fine, so it looks like I did something to break it.

    Thank you!

  21. #21
    Forum Contributor jimmalk's Avatar
    Join Date
    08-31-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    113

    Re: how can I create a macro which will check the selected criteria in filter?

    Glad to help Anand. If you found my solution helpful to you, kindly click the star on the helpful posts to give me rep points. Also mark this thread as solved. Good luck

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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