[英]Color cells based on their value - VBA
我需要能够根据VBA中的值对单元格进行颜色编码,因为条件格式将无法处理最终需要的条件数量。 例如,如果B的值是“ Decommission”,那么我想检查C,D和E的值,并根据B的值对它们进行颜色编码。不幸的是,我编写的代码贯穿整个过程表格和颜色会根据第一个值对范围内的所有内容进行编码。 我已经定义了2个范围(all_data和response),我知道出了什么问题,但是我不知道如何告诉代码仅将格式限制为行的值。任何帮助将不胜感激。
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
尝试这个:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.