【问题标题】:Is it possible to combine three rows having the same color together?是否可以将具有相同颜色的三行组合在一起?
【发布时间】:2019-10-14 08:27:51
【问题描述】:

这是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,我将非常高兴 :) 谢谢!

【问题讨论】:

  • 你的数组有什么意义?单元格是否使用条件格式着色?此外,阴影似乎毫无意义,您不能只检查 A 列中的文本是否有重复项,在这种情况下您可以使用 SUMIF 公式吗?

标签: excel vba duplicates


【解决方案1】:

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

复制C1中的公式

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

【讨论】:

    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2016-11-03
    • 2021-04-26
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多