Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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
0
ptp2
Asked:
ptp2
  • 6
  • 4
  • 2
1 Solution
 
Patrick MatthewsCommented:
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
0
 
X_layerCommented:
For that you can use conditional formatting in Excel. Which version do you use?
0
 
ptp2Author Commented:
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ptp2Author Commented:
sorry it is version 2003
0
 
X_layerCommented:
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!
0
 
Patrick MatthewsCommented:
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.)
0
 
ptp2Author Commented:
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
0
 
X_layerCommented:
Last used row on active sheet:
ActiveSheet.UsedRange.Rows.Count

Open in new window

0
 
ptp2Author Commented:
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
0
 
X_layerCommented:
This is not tested code, try it and report back please:
Sub Cond_Format_Column_E()
    Dim c As Integer
    Dim r As Integer
    Dim rMax As Integer
    For c = 5 to 5
        rMax = ActiveSheet.Cells(ActiveSheet.Rows.Count, c).End(xlUp).Row
        For r = 2 To rMax
			With ActiveSheet.Cells(r, c)
				If Not IsError(.Value) Then
					Select Case .Value
						Case < 19.5
							.Interior.ColorIndex = 3
							.Font.Bold = True
						Case >= 19.5 And < 20.01
							.Interior.ColorIndex = 0
							.Font.Color = 3
						Case > 20 Then
							.Interior.ColorIndex = 4
							.Font.Bold = True
					End Select
				End If
			End With
        Next r
    Next c
End Sub

Open in new window

0
 
ptp2Author Commented:
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
0
 
ptp2Author Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now