问题
In excel workbook project, how could we detect if the filters on some worksheet are updated?
回答1:
Make sure that you have a formula (e.g. COUNT) that includes an entire column of the data. In the case of a Table, turn on the Total row.
When the filter is changed, the Excel calculation event will fire because of the formula and you can pick this up by inserting the following code into the sheet.
Private Sub Worksheet_Calculate()
MsgBox "Calculation"
End Sub
Your sheet will need to be designed to only have data, otherwise code will be needed to determine if the calculation event on the sheet did not originate due to a change in filter.
You will need to add code to pick up the filter values. Focus on the Filter class members like Citeria1, Criteria2, Operator, On, etc.
回答2:
My case was an Excel database. I created a label indicating "number of filtered items" or "number of meeting instances", so that when you filter using dropdown filters this label will update. I didn't find any "filter change" event. I tried the method described above like the following:
- Select a cell in your sheet that you are not willing to use
- Set the formula of the cell to "=count(B:B)" or "=counta(C:C)" or any formula that depends on the entire column. Make sure that the cell is not in the same column
- Set the format type of this auxiliary cell to "custom" and set the format to ";;;" so the cell contents will be invisible
- In VBA, use the "worksheet calculate" event to execute your code
Private Sub Worksheet_Calculate()
' The commands and actions you want to execute when filter changes
End Sub
- Now, you are done
来源:https://stackoverflow.com/questions/1362342/how-to-detect-if-filters-changed-on-a-worksheet