简体   繁体   English

根据其中一个单元格的内容为整行着色

[英]Colour an entire row based on the contents of one of its cells

I'm trying to color a spreadsheet based on the results given in one of it's columns. 我正在尝试根据其中一栏给出的结果为电子表格着色。 I'm using the following code: 我正在使用以下代码:

With newSheet.Range("B:B")
    .FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "CORRECT")
    .FormatConditions(1).Interior.ColorIndex = 4

    .FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "INCORRECT")
    .FormatConditions(2).Interior.ColorIndex = 3
End With

Unfortunately this only colors the cell containing "CORRECT" or "INCORRECT". 不幸的是,这只能使包含“正确”或“错误”的单元格着色。 I'd like it to extend to the row they are in (for example, if B12 contains "CORRECT", I want A12:G12 to all be coloured green). 我希望它扩展到它们所在的行(例如,如果B12包含“ CORRECT”,我希望A12:G12都被涂成绿色)。 It was suggested that I try using an expression and so I tried the following code: 建议我尝试使用表达式,因此尝试了以下代码:

.FormatConditions.Add(Type:=XlFormatConditionType.xlExpression, Formula1:="=B" & row & "= ""CORRECT"")")
.FormatConditions(1).Interior.ColorIndex = 4

This however, returns an E_INVALIDARG exception. 但是,这将返回E_INVALIDARG异常。 I'd appreciate any tips on how to go about fixing this. 我会很感激如何解决此问题的任何提示。 I should also note that looping through every row and checking one at a time is not really an option, as there are many thousands of lines. 我还应该指出,遍历每一行并一次检查一行并不是真正的选择,因为有成千上万的行。

Your formula should work once you remove your excess closing parenthesis and make the column an absolute value 一旦删除了多余的右括号并使该列成为绝对值,您的公式就应该起作用

.FormatConditions.Add(Type:=XlFormatConditionType.xlExpression, Formula1:="=$B1= ""CORRECT""")
.FormatConditions(1).Interior.ColorIndex = 4

Make sure you set the row in your formula $B1 as the first row of your formatted range (you don't need to do a loop) 确保将公式$B1中的行设置为格式化范围的第一行(不需要循环)

You can paste this into the sheet(s) in question: 您可以将其粘贴到有问题的工作表中:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    i = 1
    While Range("B" & i).Value2 <> ""
        If Range("B" & i).Value2 = "INCORRECT" Then
            Range("A" & i & ":G" & i).Interior.ColorIndex = 3
        ElseIf Range("B" & i).Value2 = "CORRECT" Then
            Range("A" & i & ":G" & i).Interior.ColorIndex = 4
        Else
            Range("A" & i & ":G" & i).Interior.ColorIndex = 0
        End If
        i = i + 1
    Wend
End Sub

This assumes your data starts in row 1 (otherwise change the starting value of i ). 假设您的数据从第1行开始(否则更改i的起始值)。

This is a very, very low-tech answer. 这是一个非常非常低科技的答案。 But after you've got the cells highlighted in the color you need them to be (using the code), copy all the values in the column and do a paste-special for "Formats" on the rows themselves. 但是,在用颜色突出显示单元格之后,需要将其设置为(使用代码),然后复制列中的所有值,并对行本身的“格式”进行粘贴特殊操作。

Problem with that is that it'll be static, and if your values change with inputs, the coloring on the rows will be off. 问题在于它将是静态的,并且如果您的值随输入而变化,则行上的颜色将不可用。

But if it's a one-time thing, that may work. 但是,如果这是一次性的,那可能会起作用。

If you do this, make sure that the column you're evaluating has a cell format type (ie: "General", "Text", etc) that's compatible with the data in the rows you're pasting onto. 如果这样做,请确保要评估的列具有与要粘贴到的行中的数据兼容的单元格格式类型(例如:“常规”,“文本”等)。

Kludgey, but if you absolutely need this fast and you only need to do it once, it might work. 笨拙,但是如果您绝对需要快速执行此操作,并且只需要执行一次,那么它可能会起作用。

Edit: Pretty sure Kevin's answer below is a better one, as it actually solves it with code and seems like it'd work even if the values change in the evaluated cells. 编辑:可以肯定,凯文(Kevin)在下面的答案是一个更好的答案,因为它实际上是通过代码解决的,即使值在被评估的单元格中发生了变化,它也似乎可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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