简体   繁体   English

当数据发生变化时自动对 Excel 列进行排序 - 但不假设我的数据

[英]Automatically Sort Excel Columns When Data Changes - But With No Assumptions About My Data

Here is my situation:这是我的情况:

  • I have a spreadsheet in Excel 2010 setup with various columns.我在 Excel 2010 设置中有一个包含各种列的电子表格。
  • All of the columns are set to be filterable/sortable.所有列都设置为可过滤/可排序。
  • There are filters set individually on some columns.在某些列上单独设置了过滤器。
  • There is a sort set via Data > Sort.通过数据 > 排序有一个排序集。
  • The filters on the columns and the sorting are not fixed and will change.列上的过滤器和排序不是固定的,会发生变化。
  • The data can be changed in any column任何列中的数据都可以更改

How can I make the spreadsheet automatically apply the filters and sorting whenever data is changed?如何让电子表格在数据更改时自动应用过滤器和排序?

Any solution I can find online requires you to manually modify the VBA code to make it work with your specific data.我可以在网上找到的任何解决方案都需要您手动修改 VBA 代码以使其适用于您的特定数据。 You need to change things like: the range your data is in, the columns that need to be filtered by, the sorting that needs to be applied, etc, etc.您需要更改以下内容:数据所在的范围、需要过滤的列、需要应用的排序等。

Some examples that I have found that don't live up to my expectations are here , here , and here我发现的一些不符合我期望的例子在这里这里和这里

Is there a script that I could drop into any Excel spreadsheet that meets my requirements?是否有可以放入任何满足我要求的 Excel 电子表格的脚本?

I do not have possibility to check it now but adding any new criteria to existing filter and then removing it should re-filter the range.我现在无法检查它,但是向现有过滤器添加任何新标准然后删除它应该重新过滤范围。 Maybe the same would do with sorting.也许排序也是如此。
It could be triggered by worksheet_change() event.它可以由 worksheet_change() 事件触发。

This works when your filter is applied to ListObject:当您的过滤器应用于 ListObject 时,这会起作用:

Private Sub Worksheet_Change(ByVal Target As Range)
    Target.ListObject.AutoFilter.ApplyFilter
End Sub

and does the thing with sorting as well.并且也做排序的事情。

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

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