Solved

How do I colour cells depending on value

Posted on 2011-09-11
12
282 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Limit the # times a macro code will run 14 50
How do I extract data from Excel worksheets? 5 39
Need Help with Mapping and Toggling 3 21
Need help with Clear Macro 4 25
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

856 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