Conditional Formatting Fields

J Carvell

Board Regular
Joined
Sep 20, 2005
Messages
55
I am trying to find a way of turning a field on a form (which is formatted to short date) different colours depending on the day of the week. For example Monday=white, Tuesday=Yellow, Wednesday=Green etc.

Unfortunately conditional formatting doesn't give me enough conditions. I have tried to write some code found in other messages but it doesn't seem to work.

Can anyone help?
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
You can do something like this:

1. In design view, get the form's properties and go to the Events tab. In the Current event, double-click the blank line. You'll see Event Procedure. Click here, then click the Builder (...) button to go to the code window. You'll need to put in code like this --

Code:
Select Case Weekday([YourDateField])
    Case 1
       [YourDateField].BackColor=255 'red
    Case 2
       [YourDateField].BackColor=65536 'yellow, I think
    'keep going for the other 5 weekdays

End Select
To work out what numbers to use, open any form and select a range of background colours from the palette. After each one, check the BackColor property and write it down.

By putting the code into the Form_Current event, you get the formatting to apply every time you go a to new record. You'll need to ensure that the BackStyle property for the control is Solid. By default it's Transparent, and no colour change will be visible.
If you need to fire the change when a field changes, leave the Form_Current code as it is. But go to the AfterUpdate event of the control you want to use as the trigger, and type Form_Current in the code for the sub.

Denis
 

Watch MrExcel Video

Forum statistics

Threads
1,118,284
Messages
5,571,307
Members
412,381
Latest member
RogerL
Top