分享

VBA【代码】多条件汇总,汇总结果排序,多种方法,思路详解!

 冷茶视界 2024-05-14 发布于江苏

内容提要

  • 多条件汇总,汇总结果排序|完整代码

1、在工作表Sheet1里,命令按钮点击事件,调用汇总过程:

Private Sub CmdSum1_Click()    Call mySum1End Sub
Private Sub CmdSum2_Click() Call mySum2End Sub

2、在myModule里,mySum1过程,通过数组、字典、工作表排序方法,汇总数据:

Sub mySum1()    Dim ws As Worksheet, lastRow As Integer, lastCol As Integer    Dim dic As Object, dKey1, dKey2    Dim arr(), temp(), str() As String, rng As Range    Set ws = ThisWorkbook.Sheets("Sheet1")    Set dic = CreateObject("Scripting.Dictionary")    With ws        lastRow = .UsedRange.Rows.Count        lastCol = 5        arr = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)).Value    End With    For i = 2 To UBound(arr)        If arr(i, 1) <> "" Then            dKey1 = arr(i, 1) & "|" & arr(i, 2)            If Not dic.exists(dKey1) Then                dic.Add dKey1, CreateObject("Scripting.Dictionary")            End If                        For j = 3 To 5                dKey2 = arr(1, j)                dic(dKey1)(dKey2) = dic(dKey1)(dKey2) + arr(i, j)            Next        End If    Next    ReDim temp(1 To dic.Count + 1, 1 To lastCol)    For i = 1 To lastCol        temp(1, i) = arr(1, i)    Next    m = 1    For Each dKey1 In dic.keys        str = Split(dKey1, "|")        m = m + 1        temp(m, 1) = str(0)        temp(m, 2) = str(1)        For i = 3 To 5            dKey2 = temp(1, i)            temp(m, i) = dic(dKey1)(dKey2)        Next    Next    ws.Range("J1").Resize(lastRow, lastCol).ClearContents    Set rng = ws.Range("J1").Resize(UBound(temp), UBound(temp, 2))    With rng        .Value2 = temp        Call SortRange(rng, .Columns(1), .Columns(2))    End WithEnd Sub

3、在myModule里,SortRange过程,通过两个关键字对一个Range进行排序,可以选择数据是否包括标题行,默认包含

Sub SortRange( _        rng As Range, _        primarySortKey As Range, _        secondarySortKey As Range, _        Optional IncludeHeader As Boolean = True)    '//按两列排序,默认数据包含标题    Dim ws As Worksheet    Set ws = rng.Parent    With ws.Sort        .SortFields.Clear        .SortFields.Add _            Key:=primarySortKey, _            SortOn:=xlSortOnValues, _            Order:=xlAscending, _            DataOption:=xlSortNormal        .SortFields.Add _            Key:=secondarySortKey, _            SortOn:=xlSortOnValues, _            Order:=xlAscending, _            DataOption:=xlSortNormal        .SetRange rng        If IncludeHeader Then            .Header = xlYes        Else            .Header = xlNo        End If        .MatchCase = False        .Orientation = xlTopToBottom        .SortMethod = xlPinYin        .Apply    End WithEnd Sub

4、在myModule里,mySum2过程,通过数组、SortedList方法,汇总数据:

Sub mySum2()    Dim ws As Worksheet, lastRow As Integer, lastCol As Integer    Dim lst As Object, key1, key2    Dim arr(), temp(), str, rng As Range    Set ws = ThisWorkbook.Sheets("Sheet1")    Set lst = CreateObject("System.Collections.SortedList")    With ws        lastRow = .UsedRange.Rows.Count        lastCol = 5        arr = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)).Value    End With    For i = 2 To UBound(arr)        If arr(i, 1) <> "" Then            key1 = arr(i, 1) & "|" & arr(i, 2)            If Not lst.contains(key1) Then                lst.Add key1, CreateObject("System.Collections.SortedList")            End If                        For j = 3 To 5                key2 = arr(1, j)                lst.Item(key1).Item(key2) = lst.Item(key1).Item(key2) + arr(i, j)'                lst(key1)(key2) = lst(key1)(key2) + arr(i, j)            Next        End If    Next    ReDim temp(1 To lst.Count + 1, 1 To lastCol)    For i = 1 To lastCol        temp(1, i) = arr(1, i)    Next    For i = 0 To lst.Count - 1        key1 = lst.getkey(i)        str = Split(key1, "|")        temp(i + 2, 1) = str(0)        temp(i + 2, 2) = str(1)        For j = 3 To 5            key2 = temp(1, j)            temp(i + 2, j) = lst.Item(key1).Item(key2)            'temp(i + 2, j) = lst(key1)(key2)        Next    Next    ws.Range("J1").Resize(lastRow, lastCol).ClearContents    Set rng = ws.Range("J1").Resize(UBound(temp), UBound(temp, 2))    With rng        .Value2 = temp    End WithEnd Sub
~~~~~~End~~~~~~

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章