【问题标题】:MS Excel macro deleting duplicate rows and sum their valuesMS Excel 宏删除重复行并将其值求和
【发布时间】:2013-11-22 11:42:57
【问题描述】:

我对 vba 非常熟悉,但我不知道如何修改以下脚本以使其符合我的预期。

基本上我有 5 列 excel。 A 列是我想求和的值,前提是B C D E 是唯一的一行。

我找到了以下脚本,它几乎可以满足我的需要:

Option Explicit

Sub RedoDataset()
Dim LastCol As Long
Dim LastRowData As Long
Dim LastRow As Long
Dim Ctr As Long

Dim CompanyArr
Dim RowFoundArr
Dim SumArr
Dim Rng As Range
Dim SettingsArray(1 To 2) As Integer

On Error Resume Next
With Application
    SettingsArray(1) = .Calculation
    SettingsArray(2) = .ErrorCheckingOptions.BackgroundChecking
    .Calculation = xlCalculationManual
    .EnableEvents = False
    .ErrorCheckingOptions.BackgroundChecking = False
    .ScreenUpdating = False
End With
On Error GoTo 0

With ThisWorkbook
    With .Sheets("Sheet1")
        LastRowData = .Cells(Rows.Count, 1).End(xlUp).Row
        LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set Rng = .Range(.Cells(1, 1), .Cells(1, LastCol))

        .Columns(2).AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=.Cells(1, LastCol + 2), Unique:=True

        LastRow = .Cells(Rows.Count, LastCol + 2).End(xlUp).Row

        ReDim CompanyArr(1 To LastRow - 1)
        ReDim RowFoundArr(1 To LastRow - 1)
        ReDim SumArr(1 To LastRow - 1)

        For Ctr = 1 To LastRow - 1
            CompanyArr(Ctr) = .Cells(Ctr + 1, LastCol + 2)
            RowFoundArr(Ctr) = Application.Match(CompanyArr(Ctr), .Columns(2), 0)
            SumArr(Ctr) = Application.SumIf(.Columns(2), CompanyArr(Ctr), .Columns(1))
            .Cells(RowFoundArr(Ctr), 1) = SumArr(Ctr)

            Set Rng = Union(Rng, .Range(.Cells(RowFoundArr(Ctr), 1), _
            .Cells(RowFoundArr(Ctr), LastCol)))
        Next Ctr
        .Columns(LastCol + 2).Delete

        For Ctr = LastRowData To 2 Step -1
            If IsError(Application.Match(Ctr, RowFoundArr, 0)) Then
                .Rows(Ctr).Delete
            End If
        Next Ctr

    End With
End With

On Error Resume Next
With Application
    .Calculation = SettingsArray(1)
    .ErrorCheckingOptions.BackgroundChecking = SettingsArray(2)
    .EnableEvents = True
    .ScreenUpdating = True
    .ScreenUpdating = True
End With
On Error GoTo 0


End Sub

这将 A 列的值相加,使 B 列唯一。我该如何扩展它,不仅 B 是唯一的,而且条件是 - B and C and D and @ 987654329@ 在组合中是唯一的。 基本上,与其他行相比,整行是唯一的,但不一定每列都只包含唯一值:

    A    B      C      D      E
1  0.01  La    Ba     foo    boo
2  0.03  La    boo    foo    Ba
3  0.12  La    foo    Ba     boo
4  1.05  Ba    La     foo    boo

【问题讨论】:

    标签: excel vbscript unique vba


    【解决方案1】:

    试试这段代码 - 它使用了不同的方法,更灵活:

    Const cStrDelimiter As String = ";"
    
    Sub Aggregate()
        Dim dic As Object
        Dim rng As Range
        Dim strCompound As String
        Dim varKey As Variant
        Set dic = CreateObject("Scripting.Dictionary")
    
        'Store all unique combinations in a dictionary
        Set rng = Worksheets("Sheet1").Range("A1")
        While rng <> ""
            strCompound = fctStrCompound(rng.Offset(, 1).Resize(, 4))
            dic(strCompound) = dic(strCompound) + rng.Value
            Set rng = rng.Offset(1)
        Wend
    
        'Save all unique, aggregated elements in worksheet
        Set rng = Worksheets("Sheet1").Range("G1")
        For Each varKey In dic.Keys
            rng = dic(varKey)
            rng.Offset(, 1).Resize(, 4).Cells = Split(varKey, cStrDelimiter)
            Set rng = rng.Offset(1)
        Next
    End Sub
    
    Private Function fctStrCompound(rngSource As Range) As String
        Dim strTemp As String
        Dim rng As Range
        For Each rng In rngSource.Cells
            strTemp = strTemp & rng.Value & cStrDelimiter
        Next
        fctStrCompound = Left(strTemp, Len(strTemp) - Len(cStrDelimiter))
    End Function
    

    【讨论】:

    • dic(strCompound) = dic(strCompound) + rng.Value 正在这样做
    • 当然!对不起 !是的,这就像一个魅力,比我原来的快得多!谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2014-03-02
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多