close

I would like to test a cell (A1) for a number between 1 and 9.
Depending on the value returned I would like to format the cell (A1) with a
specific color
Example:

If A1 =1 then format cell yellow
If A1 = 2 then format cell blue
If A1 = 3 then format cell red
If A1 = 4 then format cell green
and so on to 9

Where do go to find the standard colors and their respective color number?

Thanks for any help

Emile

'-----------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
'-----------------------------------------------------------------
Const WS_RANGE As String = quot;H1:H10quot;

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Select Case .Value
Case 1: .Interior.ColorIndex = 6 'yellow
Case 2: .Interior.ColorIndex = 5 'blue
Case 3: .Interior.ColorIndex = 3 'red
Case 4: .Interior.ColorIndex = 10 'green
End Select
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

quot;Emilequot; gt; wrote in message
...
gt; I would like to test a cell (A1) for a number between 1 and 9.
gt; Depending on the value returned I would like to format the cell (A1) with
a
gt; specific color
gt; Example:
gt;
gt; If A1 =1 then format cell yellow
gt; If A1 = 2 then format cell blue
gt; If A1 = 3 then format cell red
gt; If A1 = 4 then format cell green
gt; and so on to 9
gt;
gt; Where do go to find the standard colors and their respective color number?
gt;
gt; Thanks for any help
gt;
gt; Emile
gt;
gt;
Bob:

Thank you very much. Works great as long as the cell value (1-9) is typed
in. In your example cell H1.

Unfortunately, my H1 value is a calculated value [Sum(f4:F31)] which changes
as more values are added in F4:F31

Is ther a way to automatically update the color as the value in the cell
changes.

Thanks again
Emile, Try this then

'-----------------------------------------------------------------
Private Sub Worksheet_Calculate()
'-----------------------------------------------------------------
Const WS_RANGE As String = quot;H1:H10quot;
Dim cell As Range

For Each cell In Me.Range(WS_RANGE)
With cell
Select Case .Value
Case 1: .Interior.ColorIndex = 6 'yellow
Case 2: .Interior.ColorIndex = 5 'blue
Case 3: .Interior.ColorIndex = 3 'red
Case 4: .Interior.ColorIndex = 10 'green
Case Else: .Interior.ColorIndex = xlColorIndexNone
End Select
End With
Next cell

End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

quot;Emilequot; gt; wrote in message
...
gt; Bob:
gt;
gt; Thank you very much. Works great as long as the cell value (1-9) is typed
gt; in. In your example cell H1.
gt;
gt; Unfortunately, my H1 value is a calculated value [Sum(f4:F31)] which
changes
gt; as more values are added in F4:F31
gt;
gt; Is ther a way to automatically update the color as the value in the cell
gt; changes.
gt;
gt; Thanks again
gt;
gt;
Bob:

That worked perfect! Thank you.
In an effort to get help with this question (which I could never have
figured out without your help) I simplified my question somewhat.
The numbers 1-9 as previously mentioned are actually not whole numbers.
They have two decimal places. So when the sum (that I am evaluating) is
anything more than a whole number in it, I get no color at all.
Example 4.65 returns no color
Example 4.00 returns the assigned color

Sorry to be a bother, but your help is greatly appreciated.

Emile
Maybe this, change the colours to suit

'-----------------------------------------------------------------
Private Sub Worksheet_Calculate()
'-----------------------------------------------------------------
Const WS_RANGE As String = quot;H1:H10quot;
Dim cell As Range

For Each cell In Me.Range(WS_RANGE)
With cell
If .Value gt;= 1 And .Value lt; 10 Then
Select Case .Value
Case Is gt; 9: .Interior.ColorIndex = 10 'green
Case Is gt; 8: .Interior.ColorIndex = 3 'red
Case Is gt; 7: .Interior.ColorIndex = 5 'blue
Case Is gt; 6: .Interior.ColorIndex = 6 'yellow
Case Is gt; 5: .Interior.ColorIndex = 6 'yellow
Case Is gt; 4: .Interior.ColorIndex = 10 'green
Case Is gt; 3: .Interior.ColorIndex = 3 'red
Case Is gt; 2: .Interior.ColorIndex = 5 'blue
Case Is gt; 1: .Interior.ColorIndex = 6 'yellow
Case Else: .Interior.ColorIndex = xlColorIndexNone
End Select
End If
End With
Next cell

End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

quot;Emilequot; gt; wrote in message
...
gt; Bob:
gt;
gt; That worked perfect! Thank you.
gt; In an effort to get help with this question (which I could never have
gt; figured out without your help) I simplified my question somewhat.
gt; The numbers 1-9 as previously mentioned are actually not whole numbers.
gt; They have two decimal places. So when the sum (that I am evaluating) is
gt; anything more than a whole number in it, I get no color at all.
gt; Example 4.65 returns no color
gt; Example 4.00 returns the assigned color
gt;
gt; Sorry to be a bother, but your help is greatly appreciated.
gt;
gt; Emile
gt;
gt;
For help with the color index numbers see
www.mvps.org/dmcritchie/excel/colors.htm
Bob:

Works exactly the way I want.

Thank you again - would be lost without your (and all people who support
these groups) assistance.

Emile
I'm going to remember that page and recommend it more often lt;vbggt;

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

quot;David McRitchiequot; gt; wrote in message
...
gt; For help with the color index numbers see
gt; www.mvps.org/dmcritchie/excel/colors.htm
gt;
gt;

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 software 的頭像
    software

    software

    software 發表在 痞客邦 留言(0) 人氣()