change cell colour using IF

joey24

New Member
Joined
Jun 12, 2008
Messages
16
how do i change the cell colour.
Eg, if the value is more than 3, the cell will show exceeded and red colour,
if the value is less than or equal to 3, the cell will show ok and green colour.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi

You can use Conditional Formatting:

eg assume your cell has the formula:

=IF(A1>3,"Exceeded",if(A1<=3,"OK","")

Then apply conditional formatting to this cell (Format>Conditional Formatting in xl2003 and below, on Home ribbon in 2007+). Set to Value Is in left hand drop box and type in the value Exceeded in right hand text box. Click Format button and choose Pattern tab and set to red. Click Add condition and ensure Value Is selected and type in OK in text box. Hit Format again, and on Pattern tab select Green. CLick OK.
 
Upvote 0
Hi,

Assuming that your workbook is the active one then this code will do the trick. Obs, this will overwrite the cells as I understood your description, if not, just color the cells next to the ones containing the values.
This code assumes that the values to look-up are situated in the A-column.
Change this to whatever column you prefer!

Code:
Sub SetCellColorFromValue()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim i As Integer, lastRow As Integer
    Dim r As Range
    
    Set wb = Application.ActiveWorkbook
    Set ws = wb.ActiveSheet
    
    Set r = ws.Cells(1, 1)
    lastRow = r.End(xlDown).Row
    
    For i = 1 To lastRow
        Set r = ws.Cells(i, 1)
        If r.Value > 3 Then
            r.Interior.ColorIndex = 3
            r.Value2 = "Exceeded"
        Else
            r.Interior.ColorIndex = 35
            r.Value2 = "OK"
        End If
    Next i
End Sub
 
Upvote 0
assume this is the formula, =IF(A1>3,"Exceeded",if(A1<=3,"OK","")
if the value is less than zero, how do i leave the cell black instead of showing "OK"?
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top