基于其值的颜色单元 - VBA

时间:2013-12-27 11:27:07

标签: excel vba colors range

我需要能够根据VBA中的值对单元格进行着色,因为条件格式化不会处理我最终需要的条件数。例如,如果B的值是"退役"那么我想检查C,D和E的值,并根据B的值对它们进行颜色编码。不幸的是,我编写的代码贯穿整个工作表并对基于范围内的所有内容进行颜色编码在第一个值。我已经定义了2个x范围(all_data和响应)我知道错误但我不知道如何告诉代码只将格式限制为行的值..任何帮助非常感谢。

Sub Formatter()

Dim All_Data As Range
Dim Response As Range

Dim MyCell As Range
Dim MyCell2 As Range

Set All_Data = Range("All_Data")
Set Response = Range("Response")

For Each MyCell In All_Data
If MyCell.Value = "Decommission" Then
   MyCell.Interior.ColorIndex = 3
         For Each MyCell2 In Response
            If MyCell2.Value = "Yes" Then
                MyCell2.Interior.ColorIndex = 4
            End If
         Next
End If
Next

End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub Formatter()

Dim All_Data As Range
Dim Response As Range

Dim MyCell As Range
Dim MyCell2 As Range

Set All_Data = Range("All_Data")
Set Response = Range("Response")

For Each MyCell In All_Data
If MyCell.Value = "Decommission" Then
   MyCell.Interior.ColorIndex = 3
         For Each MyCell2 In Intersect(Response, ActiveSheet.Rows(MyCell.Row))
            If MyCell2.Value = "Yes" Then
                MyCell2.Interior.ColorIndex = 4
            End If
         Next
End If
Next

End Sub