?
Solved

How do I colour cells depending on value

Posted on 2011-09-11
12
Medium Priority
?
286 Views
Last Modified: 2012-05-12
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
Comment
Question by:ptp2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36519806
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
 
LVL 11

Expert Comment

by:X_layer
ID: 36519808
For that you can use conditional formatting in Excel. Which version do you use?
0
 
LVL 1

Author Comment

by:ptp2
ID: 36519881
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ptp2
ID: 36519883
sorry it is version 2003
0
 
LVL 11

Expert Comment

by:X_layer
ID: 36519908
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36519909
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
 
LVL 1

Author Comment

by:ptp2
ID: 36520028
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
 
LVL 11

Expert Comment

by:X_layer
ID: 36520741
Last used row on active sheet:
ActiveSheet.UsedRange.Rows.Count

Open in new window

0
 
LVL 1

Author Comment

by:ptp2
ID: 36553371
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
 
LVL 11

Accepted Solution

by:
X_layer earned 2000 total points
ID: 36554817
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
 
LVL 1

Author Comment

by:ptp2
ID: 36555722
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
 
LVL 1

Author Closing Comment

by:ptp2
ID: 36555724
Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question