简体   繁体   English

在工作表计算后使用宏应用自动过滤器?

[英]Apply autofilter using macro after worksheet has calculated?

So I've set up this spreadsheet at work - I end up having to do a lot of fiddly Excel tasks, though I barely know any VBA - and I want a table to automatically filter itself after the worksheet has been edited. 因此,我已经在工作中设置了此电子表格-尽管我几乎不了解任何VBA,但最终不得不执行许多繁琐的Excel任务-并且我希望表格在工作表被编辑后自动进行自我过滤。 The problem is that the column which is being filtered is full of formulas, which change in response to edits made by the user, and the filter gets applied before the column has finished calculating, producing erratic results. 问题在于,要过滤的列中充满了公式,这些公式会随着用户的编辑而变化,并且在该列完成计算之前就会应用过滤器,从而产生不稳定的结果。 So really what I want is to apply AutoFilter after the worksheet has been calculated, rather than after it has been edited. 因此,我真正想要的是在计算工作表之后而不是在对其进行编辑之后应用自动筛选。

The macro I have been using is: 我一直在使用的宏是:

Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveWorkbook.Worksheets("Library").ListObjects("Library")
         .AutoFilter.ApplyFilter
    End With
End Sub

Naively changing the activating event to Worksheet_Calculate() doesn't work - it appears to be repeatedly applying the filter over and over again. 天真地将激活事件更改为Worksheet_Calculate()不起作用-它似乎是一遍又一遍地重复应用过滤器。

I'm sure this is a relatively simple one and I just don't know what it is I need to do. 我确定这是一个相对简单的方法,我只是不知道我需要做什么。 Can you guys advise? 你们可以建议吗?

(PS first time posting here - hope I have done everything right!) (PS第一次在这里发布-希望我做对了所有事情!)

Never mind, have solved it myself. 没关系,自己解决了。 For anyone else with the problem, I just set calculation to manual and then replaced my code with: 对于其他有问题的人,我只是将计算设置为手动,然后将代码替换为:

Private Sub Worksheet_Change(ByVal Target As Range)
    Calculate   
    ActiveWorkbook.Worksheets("Library").ListObjects("Library").AutoFilter.ApplyFilter
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM