Solved

How do I colour cells depending on value

Posted on 2011-09-11
12
279 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
  • 6
  • 4
  • 2
12 Comments
 
LVL 92

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
 
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 92

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now