Conditional Formatting between dates

jeckjeck_13

New Member
Joined
Jul 7, 2008
Messages
21
Hi to all,

I want to ask for help to set up an excel sheet with a formula. The scenario is like this:

We need to track the batteries if it is expired or not.Expiration is 3 months. once you received the battery, you need to put the date in the sheet (A1) and then the expiration will be automatically calculated in B1. another also, in B1, will change its color according to how may days left before the expiration date.

black color if the expiration is < 15 days
red color if the expiration is <30 days
yellow color if the expiration is between 31 and 44 days
green color if the expiration is > 45 days
white color if B1 is blank

If the expiration is <30 days, there should be an alert. it either a message will pop up or anything that will make a very good alarm.

Please help me to make this sheet.

Regards,
ernest
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
jeckjeck_13

Welcome to the MrExcel board!

The normal method to do such colouring is to use Conditional Formatting. However, until Excel 2007, there has been a limit of three conditions in Conditional formatting.

Therefore, if you have Excel 2007 you can use Conditional Formatting but if you are using an earlier version, you would need to use some vba to achieve this result. An alternative would be to reduce the number of colours required to 4 (1 standard and 3 applied by Conditional Formatting).

So if you need further help, please advise your Excel version.

Also, can you confirm that this would be happening with a list of dates down column A?
 
Upvote 0
Sir Peter,

Thanks for the reply. I have both excel 2002 and 2007. I prefer to use 2002 because most of the excel versions in our location is version 2002. Sir, there will be a lists of dates because you need to put a lot of batteries so you will see a lot of batteries listed in the sheet(Column A). Sir, please help me make the automated sheet so I can solve my problem.

Regards,
Ernest
 
Upvote 0
Ernest

There are a number of things that are not exactly clear about your requirement, but I think this should point you in the right direction. You should be able to modify this to suit just what you want. However, post back if you are still having difficulty.

1. Formulas in row 2 are copied down as far as needed.
2. Select column D by clicking its heading label and apply the conditional formatting shown for cell D1.
3. Right click the sheet name tab and choose "View Code"
4. Copy and paste the code below into the main right hand pane that opens at step 3.
5. Close the Visual Basic window.
6. Make/change some entries in column A.

Excel Workbook
ABCDEFG
1Received DateExpiry DateDays Till ExpiryWarningToady:08/07/2008
216/04/200815/07/20087ALERT
326/05/200824/08/200847
410/04/200809/07/20081ALERT
505/06/200803/09/200857
606/03/200804/06/2008Expired
716/05/200814/08/200837
806/05/200804/08/200827ALERT
Expiry Dates
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D11. / Cell Value equal to ="ALERT"Abc
D21. / Cell Value equal to ="ALERT"Abc




<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Calculate()<br>    <SPAN style="color:#00007F">Dim</SPAN> ExpiryDays <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> iColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> ExpiryDays = Range("C2", Range("C" & Rows.Count).End(xlUp))<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> ExpiryDays<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> c.Value<br>            <SPAN style="color:#00007F">Case</SPAN> "Expired", ""<br>                iColor = xlNone<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 45<br>                iColor = 4<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 30<br>                iColor = 6<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 15<br>                iColor = 3<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 0<br>                iColor = 48<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN><br>                iColor = xlNone<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>        c.Interior.ColorIndex = iColor<br>    <SPAN style="color:#00007F">Next</SPAN> c<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Hi Sir Peter,

Thanks for this... I have manage to follow your instructions and it works fine. I just want to ask if we can also highlight the "Expiration (column D)" same as Column C. And also can we add a pop up message that reminds the user how many good, near to expire or expired batteries he has?

Regards,
Ernest
 
Upvote 0
... can also highlight the "Expiration (column D)" same as Column C.
1. remove the Conditional Formatting from column D
2. Change the 4th last line of code to
Code:
c.Resize(, 2).Interior.ColorIndex = iColor


And also can we add a pop up message that reminds the user how many good, near to expire or expired batteries he has?
What, exactly, does "... good, near to expire or expired ..." mean? Does it mean the number of entries that have "Alert" in column D?

If I added the pop-up to the current code it would pop up very often and the user might get very sick of it. So... how often would you want this message to pop up? Maybe just when the sheet is opened? Or just when an entry is added/deleted/changed in column A?
 
Upvote 0
Sorry Sir if I didnt make it clear... It's better to pop-up everytime you open and close the sheet.

If the expiry date is <30 days, the color of its cell will be red

If the expiry date is between 30 and 45 days, the color of its cell will be yellow

If the expiry date is >45 days, the color of its cell will be green

What you have done is you made a column which counts the number of days left before the expiration date and by that the color is changing. Right? What I want is to eliminate this and highlight the cell which the expiry date are listed. so there will be only 2 columns... column 1 is the installation date and column number 2 is the expiry date....

Thanks a lot,

Ernest
 
Upvote 0
Some more clarification then:

1. Is there only one sheet in the workbook? If not, what is the name of this worksheet containing the dates?

If the expiry date is <30 days, the color of its cell will be red
2. Does the above mean you have dropped the condition of black if < 15 days?

3. Can you confirm that you also don't want column D like I had, and instead you just want a pop-up message when opening and closing the sheet?

4. You didn't answer my other question:
What, exactly, does "... good, near to expire or expired ..." mean?
If I don't have this clarified, I don't know what to put in the pop-up message.
 
Upvote 0
Sir,

Answers:

1) lets make 2 sheets. Sheet 1 will be for battery A and sheet 2 will be for battery B

2)<15 days = black is working fine.... no issues.. can we make it more black with white font?

3&4) pop-up message will just let us know what is the status of batteries:

Good means >45 days before expiration
Near means between 30 and 45 days before expiration
Need to replace means < 30 days before the expiration
Bad means < 15 days before the expiration.

When you open the workbook, the pop-up message will appear saying that you have: 10 good, 4 near, 3 need to replace and 1 bad batteries.

Summary:
A1 columns contains the installation dates
B1 columns contains the expiry dates

Cells in "B" column will be highlighted depending ion the conditions mentioned above.

Pop-up message will give us the summary of what is the status of the batteries. (When you open and when you close the workbook.

Sir, I hope I made it clear this time... I have added some so that it will make the sheet more hi-tech and useful.

Thanks,
Ernest
 
Upvote 0
Give this a try. Make sure you have a backup of your file first.

1. For each sheet you want to colour and check, paste this code in the VB window for that sheet.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    CheckExpiry Target, <SPAN style="color:#00007F">False</SPAN>, Me.Name<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


2. While still in the VB window, double click the "ThisWorkBook" section and paste this code.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> Worksheets<br>        CheckExpiry ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)), <SPAN style="color:#00007F">True</SPAN>, ws.Name<br>    <SPAN style="color:#00007F">Next</SPAN> ws<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()<br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> Worksheets<br>        CheckExpiry ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)), <SPAN style="color:#00007F">True</SPAN>, ws.Name<br>    <SPAN style="color:#00007F">Next</SPAN> ws<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


3. While still in the VB window, use the menu to Insert|Module and paste this code.

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CheckExpiry(<SPAN style="color:#00007F">ByRef</SPAN> myTarget <SPAN style="color:#00007F">As</SPAN> Range, <SPAN style="color:#00007F">ByVal</SPAN> OpenClose <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wsName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> Tday <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Date</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> chRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> iColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> fColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Report(2, 4) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> ReportString <SPAN style="color:#00007F">As</SPAN> String<br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> chRng = Intersect(myTarget, myTarget.Parent.Columns("A"))<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> chRng <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>        Tday = <SPAN style="color:#00007F">Date</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> 4<br>            Report(2, i) = 0<br>        <SPAN style="color:#00007F">Next</SPAN> i<br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> chRng.Offset(, 1)<br>            fColor = xlAutomatic<br>            <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> c.Value - Tday<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> <= 0, ""<br>                    iColor = xlNone<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> < 15<br>                    iColor = 1<br>                    fColor = 2<br>                    Report(2, 4) = Val(Report(2, 4)) + 1<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> < 30<br>                    iColor = 3<br>                    Report(2, 3) = Val(Report(2, 3)) + 1<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> <= 45<br>                    iColor = 6<br>                    Report(2, 2) = Val(Report(2, 2)) + 1<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> > 45<br>                    iColor = 4<br>                    Report(2, 1) = Val(Report(2, 1)) + 1<br>                <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN><br>                    iColor = xlNone<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>            c.Interior.ColorIndex = iColor<br>            c.Font.ColorIndex = fColor<br>        <SPAN style="color:#00007F">Next</SPAN> c<br>        <SPAN style="color:#00007F">If</SPAN> OpenClose <SPAN style="color:#00007F">Then</SPAN><br>            Report(1, 1) = "Good = ": Report(1, 2) = "Near = "<br>            Report(1, 3) = "Need to Replace = ": Report(1, 4) = "Bad = "<br>            ReportString = "For Sheet " & wsName & vbLf & vbLf<br>            <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> 4<br>                ReportString = ReportString & Report(1, i) & Report(2, i) & vbLf<br>            <SPAN style="color:#00007F">Next</SPAN> i<br>            Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>            MsgBox ReportString<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


4. Close the VB window, save the Workbook and try changing values in column A (you still need the formulas in column B), closing the Workbook, re-opening the Workbook.
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top