【问题标题】:Combine, Sum, and Take Highest Value based on duplicates in more than one column根据多列中的重复项组合、求和并取最大值
【发布时间】:2019-11-29 22:01:07
【问题描述】:

我有一个电子表格,我需要根据 A 列和 B 列中的值组合行。C 列需要求和,而 D 列只需要提取最高值。在示例中,我需要合并前三行(A 列和 B 列匹配),其中 C 列求和,并将 D 列返回为 1300.7,因为它是最大值。第 5 行和第 6 行需要保持不变,因为 A 列的值是重复的,但 B 列的值是唯一的。

KWSQ    FTHG    1300.7  1300.7  
KWSQ    FTHG    85  85  
KWSQ    FTHG    -85 -85  
SOBL    TFVL    150 150  
SOBL    QLLI    964.62  964.62

期望的输出

KWSQ    FTHG    1300.7  1300.7  
SOBL    TFVL    150     150  
SOBL    QLLI    964.62  964.62

我不会撒谎,我太沉迷于基于 A 列和 B 列的组合,所以我没有尝试任何东西。我找不到这方面的资源。只是为我指出如何引用 A 列和 B 列的正确方向将是我最需要的。我非常感谢任何和所有的帮助。

【问题讨论】:

  • 你需要使用vba吗?
  • 您可以使用 Excel 函数实现此目的,无需 VBA。在 F 列中复制 A 列并选择复制的数据后,从中删除重复项。然后在 I 列中,您可以应用 SumIf 之类的公式来获取值的总和。
  • @AAA VBA 确实是我所知道的唯一编程,而且我还相当新手。
  • @Mikku 我唯一的问题是这只是一个示例。实际日期是 100k 行。我还需要确保它只结合了 A 和 B 列是重复的。删除重复项会这样做吗?
  • 啊,明白了。我如何保持这样的行排列?

标签: vba sum duplicates


【解决方案1】:

以下内容可以帮助您入门。我没有测试它。我评论了代码,并在开始时提出了假设。重要提示:输入必须按前两列排序,然后按最后一列降序排序,以便最大值出现在第一行。

代码必须放在包含数据的工作表的代码窗口中,否则Cells() 函数将无法获得正确的值。

Sub Combine()

    ' Assume data is in columns A through D.
    ' Assume that data ends when column A is empty.
    ' Assume that combined data is to be written to columns F through I.
    ' Assume input data is sorted by column A, then B, and then D in descending order.

    Dim outputRowIndex As Integer
    outputRowIndex = 1

    Dim inputRowIndex As Integer
    inputRowIndex = 1

    While Not IsEmpty(Cells(inputRowIndex, 1))

        If IsEmpty(Cells(outputRowIndex, 6)) Then

            ' Copy values in columns A to D to output columns F to I.
            Cells(outputRowIndex, 6).Value2 = Cells(inputRowIndex, 1).Value2
            Cells(outputRowIndex, 7).Value2 = Cells(inputRowIndex, 2).Value2
            Cells(outputRowIndex, 8).Value2 = Cells(inputRowIndex, 3).Value2
            Cells(outputRowIndex, 9).Value2 = Cells(inputRowIndex, 4).Value2

            ' Process next input row.
            inputRowIndex = inputRowIndex + 1

        ElseIf Cells(outputRowIndex, 6).Value2 = Cells(inputRowIndex, 1).Value2 And Cells(outputRowIndex, 7).Value2 = Cells(inputRowIndex, 2).Value2 Then

            ' Cumulate values in column H from column C.
            Cells(outputRowIndex, 8).Value2 = Cells(outputRowIndex, 8).Value2 + Cells(inputRowIndex, 3).Value2

            ' Process next input row.
            inputRowIndex = inputRowIndex + 1

        Else

            ' We have a new input row. Increment outputRowIndex.
            outputRowIndex = outputRowIndex + 1

        End If
    Wend

End Sub

【讨论】:

  • 这正是我所需要的,而且很容易改变。在我这么想之后,它似乎太圆滑了。非常感谢!
【解决方案2】:

如果您的数据按字母顺序按 A 列排序,然后按 B 列排序,这将起作用:

Sub Macro1()
Dim LRow As Long, i As Long
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Sheet1") 'Change to your sheet name
    LRow = .Range("A" & .Rows.Count).End(xlUp).Row
    For i = LRow To 2 Step -1
        If .Cells(i,1) = .Cells(i-1, 1) And .Cells(i,2) = .Cells(i-1, 2) Then
            .Cells(i-1,3) = .Cells(i-1,3) + .Cells(i,3)
            .Cells(i-1,4) = Application.Max(.Cells(i-1,4), .Cells(i,4))
            .Rows(i).Delete
        End If
    Next i
End With
Application.ScreenUpdating = True
End Sub

【讨论】:

    猜你喜欢
    • 2019-05-05
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2020-12-08
    • 1970-01-01
    • 2019-02-14
    • 2017-04-08
    相关资源
    最近更新 更多