簡體   English   中英

計入每個項目的過濾器(VBA / Excel)

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

我正在處理一個處理外部報告的宏,該報告包含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

該代碼修改了您的代碼,使其擺脫了Select並添加了2個排序鍵ItemMargin column 1Auto 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM