是否可以将具有相同颜色的三行组合在一起?

时间:2019-05-28 18:55:05

标签: excel vba duplicates

这里是Image to support explanation

如果此列中的条目相同,则D列的值将以相同的颜色突出显示。

在这种情况下,对于每个“组”颜色,我应该:

  • 将给定颜色的E列的所有值相加,然后在J列中输入金额。

如您在图片中所看到的,当给定的颜色仅出现在两行中时,J列中的数量是完美的(对于红色和绿色非常适用)。 但是,一旦颜色覆盖多于两行,则总和并不反映所有行的总和,而是一次仅反映两个条目的总和。

这是我为此计算编写的代码:

Sub test()

    Dim LastRow As Long, I As Long, j As Long
    Dim arr As Variant
    Dim Total_Payments As Single
    Dim Total_CashBalance As Single

    With ThisWorkbook.Worksheets("Master")

        'Find Last row of column L
        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

        'Set array starting from row 2 to LastRow of column L
        arr = .Range("D2:D" & LastRow)

        For I = LBound(arr) To UBound(arr)

            If .Range("D" & I).Interior.Pattern <> xlNone Then

                For j = LBound(arr) To UBound(arr)

                    If (.Range("D" & j).Interior.Pattern <> xlNone) And (I <> j) Then

                        If .Range("D" & I).Interior.Color = .Range("D" & j).Interior.Color Then


                                .Range("N" & I).Value = "Cell L" & I & " has the same background color with cell/s L" & j

                                Total_Payments = .Range("E" & I).Value + .Range("E" & j).Value
                                Range("J" & I).Value = Total_Payments

                        End If

                    End If

                Next j

            End If

        Next I

    End With

End Sub

如果您能告诉我如何对一组大于2的单元格中的多个单元格进行这项工作,我将非常高兴:)谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用SUMIF公式,必要时可通过VBA插入。

抄写C1中的公式

=SUMIF($A$1:A11,A1,$B$1:$B$11)

enter image description here