保持单元格格式不变

时间:2016-10-21 12:58:58

标签: excel vba excel-vba

我有这段代码:

With .Cells(i, 4)
    If .Value > 0.8 Then
        .Interior.Color = RGB(237, 67, 55) '<-- Red color
        .Font.Color = vbWhite

        ElseIf .Value > 0.6 Then
            .Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
            .Font.Color = vbWhite

        ElseIf .Value2 = "---" Then
            .Interior.Color = .Interior.Color

        Else
            .Interior.Color = RGB(50, 205, 50) '<-- Green color
            .Font.Color = vbWhite
    End If
End With

工作表已经格式化为默认情况下具有交替的行颜色,但是在单元格的值上待处理,内部单元格颜色需要更改,等待单元格的值,但是,某些单元格没有数字数据和只是"---"。在这种情况下,我希望原始格式保持原样在代码运行之前。

基本上,如果单元格包含"---",请不要指定任何其他内部颜色,但保持已分配的颜色。

1 个答案:

答案 0 :(得分:1)

我对此进行了测试,它似乎对我的测试数据起作用,出于某种原因,excel将“---”视为大于0.8的值,因此首先移动检查“---”将停止此操作。

With .Cells(i, 4)
    If .Value = "---" Then
            .Interior.Color = .Interior.Color

        ElseIf .Value > 0.8 Then
            .Interior.Color = RGB(237, 67, 55) '<-- Red color
            .Font.Color = vbWhite

        ElseIf .Value > 0.6 Then
            .Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
            .Font.Color = vbWhite

        Else
            .Interior.Color = RGB(50, 205, 50) '<-- Green color
            .Font.Color = vbWhite

    End If
End With