提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Excel中的数据更改时自动重新应用自动筛选?

在Excel中,当你应用 筛选 函数来过滤数据,过滤结果不会随着您过滤的数据中的数据更改而自动更改。 例如,当我从数据中过滤所有苹果时,现在,我将其中一个过滤的数据更改为BBBBBB,但结果将不会像以下屏幕截图一样更改。 本文将讨论如何在Excel中的数据更改时自动重新应用自动筛选。

doc auot刷新过滤器1

使用VBA代码更改数据时自动重新应用自动筛选


箭头蓝色右泡 使用VBA代码更改数据时自动重新应用自动筛选


通常情况下,您可以通过手动单击重新应用功能来刷新过滤器数据,但在这里,我将介绍一个VBA代码,以便您在数据更改时自动刷新过滤器数据,请按照以下步骤进行操作:

1。 转到您想在数据更改时自动刷新过滤器的工作表。

2。 右键单击工作表选项卡,然后选择 查看代码 从上下文菜单中,弹出 Microsoft Visual Basic for Applications 窗口,请将以下代码复制并粘贴到空白模块窗口中,请参阅屏幕截图:

VBA代码:数据更改时自动重新应用过滤器:

Private Sub Worksheet_Change(ByVal Target As Range)
   Sheets("Sheet3").AutoFilter.ApplyFilter
End Sub

doc auot刷新过滤器2

注意:在上面的代码中, 表3的 是您使用自动过滤器的工作表的名称,请将其更改为您的需要。

3。 然后保存并关闭此代码窗口,现在,当您更改过滤的数据时, 筛选 功能将被自动刷新一次,请参阅截图:

doc auot刷新过滤器3


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Neil · 2 months ago
    Cant get this to work at all on office 365
    any suggestions
  • To post as a guest, your comment is unpublished.
    David · 5 months ago
    Hi,

    This code works great, thanks a lot.

    I, however, have one small issue with it - if I change values in any cell that is not part of the table, I am presented with Runtime error saying:

    "Run-time error '91':

    Object variable or With block variable not set up"


    I have options to Debug or End, option to Continue is greyed out. I can click on "End" and the code still works, however it is very annoying having to deal with this popup window after every change.

    Anybody has similar experience or a suggestion about how to sort this?

    Thanks!
    • To post as a guest, your comment is unpublished.
      skyyang · 4 months ago
      Hello, David,
      To solve your problem, you may apply the following code:

      Private Sub Worksheet_Change(ByVal Target As Range)
      On Error Resume Next
      Sheets("Sheet3").AutoFilter.ApplyFilter
      End Sub

      Please try it, hope it can help you!
      • To post as a guest, your comment is unpublished.
        David · 4 months ago
        Hi Skyyang,


        I have implemented your solution and it is indeed fixed.

        Thanks a lot!
  • To post as a guest, your comment is unpublished.
    joe · 5 months ago
    Brilliant and simple to do. Thanks so much!
  • To post as a guest, your comment is unpublished.
    Puly · 8 months ago
    This does not work with filter based on list selection https://www.extendoffice.com/documents/excel/4113-excel-filter-based-on-list-selection.html
  • To post as a guest, your comment is unpublished.
    Rizqi · 11 months ago
    terima Kasih

    sangat membantu
  • To post as a guest, your comment is unpublished.
    Tom · 1 years ago
    Hi, this seems to work great but I am having problems when there are more than one filter on the same worksheet (tab). I converted the range of cells to a table to allow separate and multiple filters within the same worksheet. This example only appears to update one of the tables/filters. Any suggestions on how to update ALL tables/filters within a worksheet?

    Many thanks,

    Tom
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi, Tom,
      The code in this article works well for multiple tables within a worksheet, you just need to press Enter key after changing the data instead of click to other cell.
      Please try it.
  • To post as a guest, your comment is unpublished.
    Alex · 1 years ago
    Hi, that works great, however only when manually changing data in the table.

    I have a ‘top ten/leader board’ style filtered table which is populated from data entry on a separate worksheet (actually the data goes through 3 worksheets before getting to the table). When the data is changed in the data entry worksheet the leader board table figures updates however the filter doesn’t auto refresh.
    Any ideas on how to do that?
    Much Obliged.
    Alex
  • To post as a guest, your comment is unpublished.
    Chris · 1 years ago
    This seems great. Can you tell me how to do the same for Sort, rather than Filter, please?
  • To post as a guest, your comment is unpublished.
    Steve Miller · 1 years ago
    works like a champ, and so simple. thank you very much!
  • To post as a guest, your comment is unpublished.
    Mike T · 1 years ago
    This solution works perfectly. Thanks for writing it up! If anyone is having trouble, there are a few things to consider.

    First, the Worksheet_Change event is called on a sheet-by-sheet basis. This means if you have multiple sheets which have filters you need updated, you will need to respond to all those events. One Worksheet_Change subroutine for each worksheet, not one subroutine for the entire workbook (one exception - see note below).

    Second, and a follow-on to the first, the code must be placed in the code module specific to the worksheet to be monitored. Its easy to (inadvertently) switch code modules once you get into the VB editor, so care must be taken to place it specific to the sheet you want to monitor for data changes.

    Third, this is unconfirmed, but possibly a point of error. The example uses sheet names of "Sheet1", "Sheet2", etc. If you've renamed the sheets, you may need to update the code. Note in the example, Sheet7 has been given the name "dfdf". If you wanted to update the filter there, you'd need to use;
    Sheets("dfdf").AutoFilter.ApplyFilter
    not;
    Sheets("Sheet7").AutoFilter.ApplyFilter

    It might be good to update the article including an example with a renamed sheet.


    Finally, if you want to monitor one sheet for data changes, but update filters on multiple sheets, then you only need one subroutine, placed in the code module of the worksheet you are monitoring. The code will look something like this;

    # (code must be placed in the worksheet to be monitored for data changes)
    Private Sub Worksheet_Change(ByVal Target As Range)
    Sheets("Sheet1").AutoFilter.ApplyFilter
    Sheets("Sheet2").AutoFilter.ApplyFilter
    Sheets("Sheet3").AutoFilter.ApplyFilter
    Sheets("Sheet4").AutoFilter.ApplyFilter
    End Sub
    • To post as a guest, your comment is unpublished.
      Luke H · 1 years ago
      Great explanation, thank you.

      But how do I trigger Sheets("Sheet3").AutoFilter.ApplyFilter when a new sheet is created?
      Since I cant write the code you mentioned on a sheet that doesnt exist yet
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Mike,
      Thanks for your detailed explanation.
  • To post as a guest, your comment is unpublished.
    Dave J · 2 years ago
    Works great and saves me a lot of time and messing about.. Really great tip.. Many thanks for your help
  • To post as a guest, your comment is unpublished.
    Asad · 2 years ago
    this command all fake do nothing . totally try but no use of.
  • To post as a guest, your comment is unpublished.
    SABRINA · 2 years ago
    I cannot get this to work for me at all. I am trying to take from a master sheet and have it only take the jobs that apply to certain project managers on each tab that is with their names. I also want it to auto refresh when I make changes.
  • To post as a guest, your comment is unpublished.
    Brian · 2 years ago
    I am doing this for a front in sheet were it the cell is set to =sheet1!E6. It will not apply filter when it changes. If i change the number in the back sheet it adjust front but does not filter. If adjust the formula to filter it criteria it does reapply. What can i do?
    • To post as a guest, your comment is unpublished.
      Asad · 2 years ago
      Use this
      Private Sub Work_Change(ByVal Target As Range)
      Activesheet.AutoFilter.ApplyFilter
      End Sub
  • To post as a guest, your comment is unpublished.
    Drew · 2 years ago
    I I want a change on one sheet to cause multiple other sheets to autofilter, how do I change this code? Ex: SheetA is changed, which causes Sheet1, Sheet2, and Sheet3 to apply its autofilter.

    Thanks!
  • To post as a guest, your comment is unpublished.
    Arif · 2 years ago
    Nice.. really i need it
  • To post as a guest, your comment is unpublished.
    VINICIUS · 2 years ago
    hello, how can i use all this in google finance?

    Tks