【问题标题】:Is there a way to loop over cells of a particular color in a column?有没有办法遍历列中特定颜色的单元格?
【发布时间】:2019-10-14 01:02:27
【问题描述】:

我在电子表格中有一个列(L 列),其中有许多条目,该列中有一些重复值。每个“组”重复值都以唯一的颜色着色。所以我的列中有很多颜色,每种颜色都指一组重复值。我必须遍历每个“组”,以便所有单元格都用相同的颜色着色并进行一些计算。但是,我不知道如何遍历列中相同颜色的所有单元格。

如果你能帮助我,我会很高兴:)

【问题讨论】:

  • 你使用条件格式吗?
  • 您将看到单元格的interior。这可能是通过rgbcolor 等。到目前为止,您有任何尝试过的代码吗?这将有助于给出更直接的答案,例如“你是如何做到的?”会被认为是主观的,不适合这个旨在回答客观问题的论坛。
  • 你可以按颜色排序,也许这样可以简化你的问题
  • 你能发一张它的样子吗?
  • 您也可以尝试使用Find-命令并使用Application.FindFormat定义您搜索的颜色

标签: excel vba duplicates


【解决方案1】:

你可以试试:

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long, j As Long
    Dim arr As Variant

    With ThisWorkbook.Worksheets("Sheet1")

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

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

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

            If .Range("L" & i).Interior.Pattern <> xlNone Then

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

                    If (.Range("L" & j).Interior.Pattern <> xlNone) And (i <> j) Then

                        If .Range("L" & i).Interior.Color = .Range("L" & j).Interior.Color Then

                            If .Range("N" & i).Value = "" Then
                                .Range("N" & i).Value = "Cell L" & i & " has the same background color with cell/s L" & j
                            Else
                                .Range("N" & i).Value = .Range("N" & i).Value & ", L" & j
                            End If

                        End If

                    End If

                Next j

            End If

        Next i

    End With

End Sub

结果:

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2021-04-05
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多