【问题标题】:Count in filter per item (VBA/Excel)每个项目的过滤器计数(VBA/Excel)
【发布时间】:2019-02-17 06:05:45
【问题描述】:

我正在处理一个处理外部报告的宏,该报告包含 75,000 条记录。我需要找到一种方法来过滤每个项目并根据每个项目的最大计数对项目进行排序。这是一个例子:

如果您需要更多详细信息,请告诉我,感谢您的关注。

一个好主意应该为每个项目应用一个过滤器,以便根据值的最大计数进行排序和排序。

With dataSheet
    .Activate
    .Range("A1:B1").Select
    .Range("A1:B1").AutoFilter
    .AutoFilter.Sort.SortFields.Clear
    .AutoFilter.Sort.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending (Instead of Descending add a count and filter based on the max count), DataOption:=xlSortNormal
    With .AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
End With

【问题讨论】:

  • 看起来简单的排序就可以完成这项工作。到目前为止,您尝试过什么?
  • 我尝试使用索引公式,但要花很长时间来计算每个项目。我正在考虑应用自动过滤器 .Range("A1:S1").AutoFilter .AutoFilter.Sort.SortFields.Clear .AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues , Order:=xlDescending, DataOption:=xlSortNormal With .AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply 结束
  • 但不是降序,而是应用公式计算并根据最大值过滤
  • 当您想添加信息时,请edit您的问题而不是使用cmets。在 cmets 中解析代码真的很难
  • 哪个先出现?过滤器还是排序?过滤器是否基于某种聚合?还是什么规则?代码中的排序似乎只基于一列——那一列标题是什么?为什么最终输出似乎是按两列排序的?

标签: vba excel sorting filter


【解决方案1】:

此代码修改您的代码以摆脱 Select 并添加 2 个排序键,ItemMargincolumn 1 中的 Auto filter 允许您过滤单个项目。您可以更改工作表以满足您的需求。

Dim lRow As Long
lRow = Range("A" & Rows.Count).End(xlUp).Row

    With ThisWorkbook.Worksheets("Sheet1")
        .Columns(1).AutoFilter

        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("A2:A" & lRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SortFields.Add Key:=Range("B2:B" & lRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

            .SetRange Range("A2:B" & lRow)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    相关资源
    最近更新 更多