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
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
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
Put this into a cell : FilterCriteria(# of columns in your table, Table Header row)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
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 :
Pressing the button will clear the filter from your table.ActiveSheet.ShowAllData
Last edited by jimmalk; 11-04-2012 at 11:26 PM.
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?
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
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.
Last edited by jimmalk; 11-04-2012 at 11:53 PM.
let me try this and I will post if it works or not, Thanks!
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?
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.
Still it does not show anything, I only have one filter criteria selected which is one animal name.
check the attached spreadsheet, it still does not show any thing in cell "Q5".
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 :
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 B2Application.Calculation = xlAutomatic
Last edited by jimmalk; 11-05-2012 at 01:11 AM.
Thank you, I think I will spend some time on this and let you know by tomorrow if I have any more questions, Thanks!
You're welcome Anand. I'll watch the post. Good luck
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!
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
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 .
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.
I did check it and "Calculation Options" is set to "Automatic". any other ideas?
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.
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!
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks