简体   繁体   English

计入每个项目的过滤器(VBA / Excel)

[英]Count in filter per item (VBA/Excel)

I'm just working on a Macro that process a external report, this report contains 75,000 records. 我正在处理一个处理外部报告的宏,该报告包含75,000条记录。 I need to find out a way to filter per item and sort the items based on the maximum of counts each of them has. 我需要找出一种方法来过滤每个项目,并根据每个项目具有的最大计数对项目进行排序。 This is an example: 这是一个例子:

在此处输入图片说明

Let me know if you need more details and thanks for your attention. 让我知道是否需要更多详细信息,感谢您的关注。

A good idea should be apply a filter per item in order to sort and order based on the maximum count of a value. 最好是对每个项目应用一个过滤器,以便根据值的最大数量进行排序和排序。

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

This code modifies yours to get rid of Select and adds 2 sort keys, Item & Margin . 该代码修改了您的代码,使其摆脱了Select并添加了2个排序键ItemMargin The Auto filter in column 1 , allows you to filter the individual items. column 1Auto filter允许您过滤单个项目。 You can change your worksheet to meet your needs. 您可以更改工作表以满足您的需求。

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

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

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