跳到主要內容

當Excel中的數據更改時,如何自動重新應用自動篩選?

在Excel中,當您應用 篩選 過濾數據的功能,過濾結果不會隨著過濾數據中數據的更改而自動更改。 例如,當我從數據中過濾所有Apple時,現在,我將過濾後的數據之一更改為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 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (39)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Finally got the filter to update automatically. My scenario included a filter on sheet "Master" that had a formulas that depended on other sheets (named 1, 2,...,21).

Paste this in the source code into the "Master" sheet.

Private Sub Worksheet_Calculate()
On Error Resume Next
Application.EnableEvents = False
Me.AutoFilter.ApplyFilter
Application.EnableEvents = True
End Sub


Paste this in the source code of each sheet that feeds data into the master sheet. (Sheet 21 shown from my scenario)

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Sheets("Master").AutoFilter.ApplyFilter
End Sub
This comment was minimized by the moderator on the site
Its not working if able changing by itslef, like when you linking data from another app, can someone help me fix this please
This comment was minimized by the moderator on the site
Pasted it as shown, but not working. The sheet I need updated is an invoice that has formulas that are pulling from another sheet. Since the blank cells all have formulas in them, is that preventing it from working? Seems simple for it to work, so I'm getting frustrated with myself.
This comment was minimized by the moderator on the site
2022 update!
Just add this to the sheets you want it applied too.

Private Sub Worksheet_Change(ByVal Target As Range)
AutoFilter.ApplyFilter
End Sub
Rated 5 out of 5
This comment was minimized by the moderator on the site
My input data is on the first sheet of the book, and the table in which the filter is applied is on another sheet of the same book. When I change the data on the first sheet, the filter on the second sheet should be updated. But if at the time of changing the data, a cell in the range of this table is not selected, then I get an error:"Run-time error '91': Object variable or With block variable not set". Therefore, i am should always check that the cursor is in a table range cell and not in any other place on the sheet. How to fix it?Solved:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("name of sheet").ListObjects("name of table").AutoFilter.ApplyFilter
End Sub
This comment was minimized by the moderator on the site
Thank you. Works great!
This comment was minimized by the moderator on the site
Hey this works great but I'd like to re-apply the autofilter to multiple sheets if possible.
I have 3 sheets (Enquiry, Booked, No sale)

What I'm trying to do is essentially move the data when I change the item status code:
"Enquiry" is filtered to show 'In progress' only, "Booked" is filtered to show 'Booked' only etc

Is there a way to re-apply filter to multiple worksheets when making a change on "enquiry"
This comment was minimized by the moderator on the site
I actually have data from an other Excel file that got imported in a Excelsheet with the name "Database". Then I import this data in the same Excel file but in an other ExcelSheet "Overview". I want when the data changes in the orgininal source, that the filter applies in the sheet "Overview". Thank you in forward for the one who can help me :). P.S. cant use VBA in the firt excelsheet
This comment was minimized by the moderator on the site
Hi,

This is a great bit of code thank you. The only issue I am having is I'm using a drop down on a separate chart sheet. If I manually change the value in the cell associated with the drop down, it works. But when I try to just use the drop down, it won't update. Any thoughts?
This comment was minimized by the moderator on the site
Hi, thanks so much for the help. Something isn't working right for me. Here's the story.

Sheet1 has variable data. Sheet3 has static data and filter. Filter criteria on "Sheet3" comes from Sheet1. Sheet1 has data that comes from filtered results on Sheet3.

Sheet3 has code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("A1:U14").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("A22:U23"), CopyToRange:=Range("A25:U26"), Unique:=False
End Sub

It works great if I do anything on Sheet3. No problems. Thank you!

At first I had code on Sheet1:

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

Which resulted in the error "Runtime error 91, Object Variable or With Block not Set".

I changed the code based on comments to be:

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

Now I don't get an error, but the data on Sheet3 and therefore Sheet1 don't change. In other words, the event of applying the filter to Sheet3 doesn't occur when I make a change on Sheet1. It doesn't matter if I hit <return> or click on another cell after changing the Sheet3 filter criteria cell that is set on Sheet1.

As an aside, I expect that if I wanted to have multiple cells on Sheet1 that caused filters on Sheets 4 and 5 in addition to Sheet3, I would need the code on Sheet 1 to read:

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

Thanks again!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations