Link to home
Start Free TrialLog in
Avatar of ptp2
ptp2Flag for Ireland

asked on

How do I colour cells depending on value

I need a vba module to colour all used cells in column E (E2 - end of data) E1 contains a header) depending on the following criteria:
If cell value less than 19.5 then cell font color = Red and bold
or
If cell value greater than 20 then cell font color = Green and bold
else cell font color = Black
Thanks
Peter
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

You can do this without VBA.

1) Select E2:E65536 (E2:E1048576 in Excel 2007 or 2010)

2) In Excel 2003, select Format / Conditional Formatting from the menu, or in Excel 2007/2010 select Conditional Formatting from the Home tab of the Ribbon

3) Create a Conditional Formatting formula-based rule using the formula =E2<19.5 and select your fill color and font formatting

4) Create another CF formula-based rule using =E2>20, and again pick your formatting
For that you can use conditional formatting in Excel. Which version do you use?
Avatar of ptp2

ASKER

Thanks for your responses I know exactly what to do manually but I need a vba module as I am formatting as small part of a very large macro that has no user input.
Thanks
Peter
Avatar of ptp2

ASKER

sorry it is version 2003
Try this:
Sub Check_Range_Value() 
Dim rnArea As Range 
Dim rnCell As Range 

Set rnArea = Range("A1:B11") 

  For Each rnCell In rnArea 
     With rnCell 
        If Not IsError(.Value) Then 
      Select Case .Value 
          Case 0 to 19.5 
             .Interior.ColorIndex = 3
             .Font.Bold = True
          Case 19.51 to 19.99 
            .Interior.ColorIndex = 1
          Case 20 to 100 
             .Interior.ColorIndex = 4
             .Font.Bold = True 
      End Select 
       End If 
   End With 

Open in new window


This code isn't tested!
Then I suggest that you do the manual steps above while the macro recorder is on, and post the recorded code here for us to help you clean it up.

(I do not have Excel 2003 available to me, so I cannot do it for you.  There have been significant changes to CF since then.)
Avatar of ptp2

ASKER

X_layer
I tested your code and got it to work by changing the range (see below) I now need to have the code select from the second row to the last row of the used cells i.e. (E2:lrow) as the data changes everything the macro runs.
Hope this makes sense.
Thanks
Peter

Code:
Sub Check_Range_Value()
Dim rnArea As Range
Dim rnCell As Range

Set rnArea = Range("E2:E99")

  For Each rnCell In rnArea
     With rnCell
        If Not IsError(.Value) Then
      Select Case .Value
          Case 0 To 19.5
             .Interior.ColorIndex = 3
             .Font.Bold = True
          Case 19.51 To 19.99
            .Interior.ColorIndex = 0
            .Font.Color = 3
          Case 20 To 10
             .Interior.ColorIndex = 4
             .Font.Bold = True
      End Select
       End If
   End With
   Next

End Sub
Last used row on active sheet:
ActiveSheet.UsedRange.Rows.Count

Open in new window

Avatar of ptp2

ASKER

Hi X_layer,
Sorry for the delay in replying - health issues - I worked with some code I used before that populates all used cells in a column and came up with this code. It is rather clumsy and I like your use of the Select Case method, would you take a look and see if you can incorporate your code to make it neater? in other words does this module make you cringe!
Thanks
Peter

Sub Cond_Format_Column_E()
   
    Dim c As Integer
    Dim r As Integer
    Dim rMax As Integer
    For c = 5 To 5    'col control
        rMax = ActiveSheet.Cells(ActiveSheet.Rows.Count, c).End(xlUp).Row  'last used row in col
        For r = 2 To rMax
            If ActiveSheet.Cells(r, c) = "" Then    'if empty, ignore
            Else
            If ActiveSheet.Cells(r, c) < 19.5 Then    'if less than 19.5 shade Red and Bold
               ActiveSheet.Cells(r, c).Interior.ColorIndex = 3   'shade Red
               ActiveSheet.Cells(r, c).Font.Bold = True
            Else
            If ActiveSheet.Cells(r, c) >= 19.5 And ActiveSheet.Cells(r, c) < 20.01 Then     'if greater than 20 shade Green and Bold
               ActiveSheet.Cells(r, c).Interior.ColorIndex = 0   'shade Green
               ActiveSheet.Cells(r, c).Font.Bold = True
            Else
            If ActiveSheet.Cells(r, c) > 20 Then      'if greater than 20 shade Green and Bold
               ActiveSheet.Cells(r, c).Interior.ColorIndex = 4   'shade Green
               ActiveSheet.Cells(r, c).Font.Bold = True

            End If
            End If
            End If
            End If
           
        Next r
    Next c
End Sub
ASKER CERTIFIED SOLUTION
Avatar of X Layer
X Layer
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptp2

ASKER

Hi X_layer,
Tested your code and it worked a charm, exactly as I wanted - cleaner code - thank you for your patience, enjoy you points!
Thanks again
Peter
Avatar of ptp2

ASKER

Thank you