Cond Format - 7 conditions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to set more than 3 conditional formats? I want the user
to be able to type in a number and date in a cell like so:

1 04/05/07

and based on the number in the left most position, shade the cell a specific
color. I would have up to 7 different color shades.

Thanks,

Sarah
 
Here is some VBA code (courtesy of Bob Phillips) which will
allow you to code more than 3 conditions:


'-----------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
'-----------------------------------------------------------------
Const WS_RANGE As String = "H1:H10" '<=== change to suit


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 = 3 'red
Case 2: .Interior.ColorIndex = 6 'yellow
Case 3: .Interior.ColorIndex = 5 'blue
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
 
Example.


'-----------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
'-----------------------------------------------------------------
Const WS_RANGE As String = "H1:H10" '<=== change to suit

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 = 3 'red
Case 2: .Interior.ColorIndex = 6 'yellow
Case 3: .Interior.ColorIndex = 5 'blue
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

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
That worked great, but once I import the Excel worksheet into MS Access and
then export it back out to my Excel template with the code you supplied, I
don't get the colors in the cell unless I go into each cell and hit enter.
How can they automatically show the color with out me having to do this?

Thanks,

Sarah
 
The code (worksheet event) does NOT work if you copy/paste (import) data: it
only works if the data is entered manually. Hence it works when you hit enter
as this triggers the worksheet event code.

The only option I can think of is to write code which will loop through ALL
cells in your range and set the conditional format.
 
Pleased to know it provided a satisfactory solution. Thank you for the
feedback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top