Question

I need to change the color of a cell in B row if a value is entered in a corresponding cell in A row. I did this using the formula in conditional formatting

=OR(COUNTA($H3:$AG3)>5,COUNTA($H3:$AG27)>5* COUNTA($N3:$N27)) applies to -=$N$3:$O$27 How to do this in vbt code for it. Thanks in advance.

Was it helpful?

Solution

You can use a while loop to go through each row or column, while there are values.

You can use Font.Color to set an RGB value (e.g. 255, 0, 0 for red) for the font.

You can use Interior.Color to set an RGB value (e.g. 0, 0, 255 for blue) for the cell background.

Below is an example of using these to loop through rows, changing the colour of the text in column B to green if the value in column A is 1.

Do While Len(Range("A1").Offset(counter, 0).Value) > 0

    If Range("A1").Offset(counter, 0).Value = 1 Then
        Range("B1").Offset(counter, 0).Interior.Color = RGB(0, 255, 0)
    End If
    counter = counter + 1

Loop

Hopefully you can use this a basis for your needs. If you wanted to loop through columns rather than columns for example just change the way the Offset is used, so the counter variable is the second argument, e.g. Offset(0, counter)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top