简体   繁体   中英

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). 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. 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)

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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