【问题标题】:how to combine duplicate rows and sum the values 3 column in excel如何在excel中合并重复行并对值3列求和
【发布时间】:2016-08-17 17:55:31
【问题描述】:

大家好, 我在创建 VBA excel 以复制数据时遇到问题。

如何在excel中合并重复行并对3列的值求和?

谢谢。

【问题讨论】:

  • 你想用vba还是直接公式?两者都有可能
  • 我想用 VBA Excel 来做。感谢您的信息

标签: arrays vba duplicates


【解决方案1】:

这个使用删除重复项:

Sub dupremove()
Dim ws As Worksheet
Dim lastrow As Long

Set ws = Sheets("Sheet1") ' Change to your sheet

With ws
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    With .Range("B2:C" & lastrow)
        .Offset(, 4).FormulaR1C1 = "=SUMIF(C1,RC1,C[-4])"
        .Offset(, 4).Value = .Offset(, 4).Value
    End With
    With .Range("A1:A" & lastrow)
        .Offset(, 4).Value.Value = .Value
    End with
    .Range("E1:G" & lastrow).RemoveDuplicates 1, xlYes

End With

End Sub

【讨论】:

  • 很好的解决方案。只需将RC[-26] 替换为C[-26]。在 OP 澄清维护原始数据后,我采纳了您的想法来编辑我的答案。所以感谢你!
  • @user3598756 感谢您的关注。我已经编辑移动到一边。
  • @GpowerGravityPower 看到编辑它现在发布到一边。
【解决方案2】:

在 OP 澄清后编辑

试试这个

维护原始数据的解决方案:

Option Explicit

Sub main()

With Worksheets("Sheet01") '<== change "Sheet01" as per your actual sheet name

    With .Range("A1:C1").Resize(.Cells(.rows.Count, 1).End(xlUp).Row)
        .Copy
        With .Offset(, .Columns.Count + 1)
            .PasteSpecial xlPasteAll ' copy value and formats
            .Columns(2).Offset(1).Resize(.rows.Count - 1, 2).FormulaR1C1 = "=SUMIF(C1,RC1,C[-" & .Columns.Count + 1 & "])"
            .Value = .Value
            .RemoveDuplicates 1, xlYes
        End With
    End With

End With
End Sub

覆盖原始数据的解决方案(留作参考):

Sub main()
Dim helperRng As Range, dataRng As Range
Dim colToFilter As String
Dim colsToSumUp As Long

With Worksheets("Sheet01") '<== change "Sheet01" as per your actual sheet name
    Set dataRng = .Range("A2:C2").Resize(.Cells(.rows.Count, 1).End(xlUp).Row - 1)
    colToFilter = "A" ' set here the column header you want to sum up on
    colsToSumUp = 3 ' number of adjacent columns to sum up with
    Set helperRng = dataRng.Offset(, .UsedRange.Columns.Count + 1).Resize(, 1) 'localize "helper" cells first column out of sheet used range
    With helperRng
        .FormulaR1C1 = "=RC" & Cells(1, colToFilter).Column 'make a copy of the values you want to sum up on
        .Offset(, 1).FormulaR1C1 = "=if(countif(R1C[-1]:RC[-1], RC[-1])=1,1,"""")" 'localize with "1" first occurrence of each unique value
        With .Offset(, 2).Resize(, colsToSumUp)
            .FormulaR1C1 = "=sumif(C" & helperRng.Column & ", RC" & helperRng.Column & ",C[" & Cells(1, colToFilter).Column - helperRng.Column - 1 & "])" 'sum up in adjacent columns
            .Value = .Value 'get rid of formulas
        End With
        .Offset(, 1).SpecialCells(xlCellTypeFormulas, xlTextValues).EntireRow.Delete 'delete rows with repeted values you want to sum up on
        dataRng.Columns(2).Resize(.rows.Count, colsToSumUp).Value = .Offset(, 2).Resize(.rows.Count, colsToSumUp).Value 'copy summed up values from "helper" cells
        helperRng.Resize(, 1 + 1 + colsToSumUp).Clear 'clear "helper" cells
    End With

End With

End Sub

已注释,以便您可以遵循代码并适应您的实际数据“结构”

【讨论】:

  • 感谢您的好意。如果我想保持原始数据。通过添加数据处理。放置原始数据的一侧,看图片。再次感谢
  • 然后采用 Scott Craner 的解决方案,您只需将 RC[-26] 替换为 C[-26]
  • 实际上我看到 Scott Craner 的解决方案也没有维护原始数据。但它有非常好的想法。所以我拿走了它们并编辑了我的帖子,添加了一个维护原始数据的解决方案。归功于斯科特!
猜你喜欢
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
相关资源
最近更新 更多