寻找具有特定内部颜色的细胞

时间:2017-06-02 19:27:48

标签: excel vba excel-vba

我正在使用此代码来定位特定颜色的单元格(通过条件格式更改),然后根据行中的值交换到另一个工作表。但是,宏运行,只是没有找到任何东西。没有错误消息,它只是找不到任何单元格(我已经切换到RGB 255,0,0进行此测试)。我在这里做错了什么?

Sub ChangeAccountDetailsForMay()

Dim ws As Worksheet
Set ws = Sheets("comparison")
Dim destws As Worksheet
Set destws = Sheets("Account Detail")
Dim i As Integer
Dim j As Integer

For i = 24 To 3205
    If ActiveWorkbook.Sheets("comparison").Range("BF" & i).Interior.Color = RGB(255, 0, 0) Then 'Might be RGB 218 148 148
        MsgBox "Found one at row " & i & "!"
        For j = 25 To 3077
            If ActiveWorkbook.Sheets(destws).Range("J" & j).Value = ActiveWorkbook.Sheets(ws).Range("J" & i).Value And ActiveWorkbook.Sheets(destws).Range("L" & j).Value = ActiveWorkbook.Sheets(ws).Range("L" & i).Value Then
                ActiveWorkbook.Sheets(destws).Range("BD" & j).Value = ActiveWorkbook.Sheets(ws).Range("BB" & i).Value
                ActiveWorkbook.Sheets(destws).Range("BE" & j).Value = ActiveWorkbook.Sheets(ws).Range("BC" & i).Value
                ActiveWorkbook.Sheets(destws).Range("BF" & j).Value = ActiveWorkbook.Sheets(ws).Range("BD" & i).Value
            End If
        Next j
    End If
Next i

MsgBox "Done!", vbInformation, "Success!"
End Sub

1 个答案:

答案 0 :(得分:2)

要获取条件格式提供的单元格颜色,必须使用DisplayFormat

...Range ("BF" & i).DisplayFormat.Interior.Color...