Chủ đề này là cổ xưa, nhưng tôi không hài lòng với bất kỳ câu trả lời nào, và cuối cùng tôi tự viết. Tôi đang chia sẻ nó ngay bây giờ:
Chúng tôi bắt đầu với:
Sub ResetWSFilters(ws as worksheet) If ws.FilterMode Then ws.ShowAllData Else End If 'This gets rid of "normal" filters - but tables will remain filtered For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj 'And this gets rid of table filters End SubChúng ta có thể cung cấp một bảng tính cụ thể cho macro này, nó sẽ chỉ lọc ra một bảng tính đó. Hữu ích nếu bạn cần đảm bảo chỉ cần một bảng tính là rõ ràng. Tuy nhiên, tôi thường muốn làm toàn bộ sách bài tập
Sub ResetAllWBFilters(wb as workbook) Dim ws As Worksheet Dim wb As Workbook Dim listObj As ListObject For Each ws In wb.Worksheets If ws.FilterMode Then ws.ShowAllData Else End If 'This removes "normal" filters in the workbook - however, it doesn't remove table filters For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj Next 'And this removes table filters. You need both aspects to make it work. End SubBạn có thể sử dụng điều này, ví dụ, bằng cách mở một sổ làm việc bạn cần xử lý và đặt lại các bộ lọc của chúng trước khi làm bất cứ điều gì với nó:
Sub ExampleOpen() Set TestingWorkBook = Workbooks.Open("C:Intel......") 'The .open is assuming you need to open the workbook in question - different procedure if it's already open Call ResetAllWBFilters(TestingWorkBook) End SubCông cụ tôi sử dụng nhiều nhất: Đặt lại tất cả các bộ lọc trong sổ làm việc mà mô-đun được lưu trữ trong:
Sub ResetFilters() Dim ws As Worksheet Dim wb As Workbook Dim listObj As ListObject Set wb = ThisWorkbook 'Set wb = ActiveWorkbook 'This is if you place the macro in your personal wb to be able to reset the filters on any wb you're currently working on. Remove the set wb = thisworkbook if that's what you need For Each ws In wb.Worksheets If ws.FilterMode Then ws.ShowAllData Else End If 'This removes "normal" filters in the workbook - however, it doesn't remove table filters For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj Next 'And this removes table filters. You need both aspects to make it work. End Sub