获取所有特定颜色的Excel单元格

时间:2019-02-26 21:53:41

标签: python excel colors automation com

我正在自动测试套件中的特定零件。其中一项包括将数据填充到特定颜色的excel单元中。我可以使用UsedRange.Find代替for循环迭代器来搜索该颜色的单元格吗?我真的不想遍历每个单元格并执行cell.interior.colorindex,而是一个最佳查询。我使用的语言是python,因为我在测试工具IDE中,所以我无法访问xlrd。这就是全部COM。

1 个答案:

答案 0 :(得分:0)

更新!
是的,您可以使用sheets(..).usedrange.find ...
我今天无法重现sheets(..).usedrange.find的结果!

以下(精简和更正)的vba代码示例:

Option Explicit

Sub Find_Yellow()
    Dim firstFind As Range
    Dim iCt As Integer
    For iCt = 1 To 2
        If iCt = 1 Then Range("C15").Interior.Color = -4142 'xlNone   = -4142
        If iCt = 2 Then Range("C15").Interior.Color = 65535 'vbyellow = 65535
        Call SetInteriorSearchColor(65535) 'vbyellow = 65535
        Set firstFind = Nothing
        On Error Resume Next
            Set firstFind = Sheets(1).Cells.Find("", Cells(1, 1), -4123, 2, 1, 1, False, , True)
        On Error GoTo 0
        If firstFind Is Nothing Then
            Debug.Print "round " & iCt & ": Yellow filled cell NOT found!"
        Else
            Debug.Print "round " & iCt & ": Yellow filled cell found at " & firstFind.Address & "!"
        End If
    Next iCt
End Sub

Sub SetInteriorSearchColor(IscColor As Long)
    With Application.FindFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = IscColor 'vbyellow = 65535 'orange = 49407
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub



立即窗口

round 1: Yellow filled cell NOT found!
round 2: Yellow filled cell found at $C$15!
相关问题