简体   繁体   English

基于其值的颜色单元-VBA

[英]Color cells based on their value - VBA

I need to be able to colour code cells based on their values in VBA as conditional formatting will not handle the number of conditions I will ultimately require. 我需要能够根据VBA中的值对单元格进行颜色编码,因为条件格式将无法处理最终需要的条件数量。 For instance if the value of B is "Decommission" then I would like to check the values of C, D and E and colour code them in relation to the value of B. Unfortunately, the code I've written is running through the whole sheet and colour coding everything in the range based on the first value. 例如,如果B的值是“ Decommission”,那么我想检查C,D和E的值,并根据B的值对它们进行颜色编码。不幸的是,我编写的代码贯穿整个过程表格和颜色会根据第一个值对范围内的所有内容进行编码。 I've defined 2 x ranges (all_data and response) I know what's wrong but I don't know how to tell the code to only restrict the formatting to the value of the row.. Any help would be greatly appreciated. 我已经定义了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

Try this: 尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM