VBA to Change front to red if within 30 days

Tmoske

Board Regular
Joined
Jan 14, 2009
Messages
145
How can I use a VBA to change the font color to red for all the dates on a sheet that are within 30 days of today?

Thanks
Tmoske
 
Hmmm... I take it "works great" was referring to the code then?

Okay - presuming there's not a bajillion date cells, I still agree w/Norie that conditional formatting (CV) would be simpler.

Try this in a blank/new workbook:

Select Cell A1

Menubar | Format | Conditional Formatting...

In the drop-down box under Condition1, change to Formula Is.


Type in (WITHOUT the quote marks):
Code:
<TODAY()+30
<TODAY()+30"< p>
Note: Excel will insert the equals sign and the quote marks for you.

Now, click the Format... button, make sure the Font tab is active, and change the color.

Click <OK>for both dialogs, and if you type "03/15/09" (or whatever format you use for dates), the font will change to red.

I have to get for a while, so in case you are still having problems with the CV, this would change the dates back to black if the user overwrote with an 'acceptable' date:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngMyUsedRange As Range
Dim rCell As Range
 
    Set rngMyUsedRange = Range("A1:N40")
 
 
    For Each rCell In rngMyUsedRange
        If IsDate(rCell.Value) _
        And rCell.Value < Date + 30 Then
            rCell.Font.Color = &HFF&
        ElseIf IsDate(rCell.Value) _
        And rCell.Value >= Date + 30 Then
            rCell.Font.Color = &H0&
        End If
    Next
End Sub

Hope that helps,

Mark
 
Last edited:
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
ACK! Ten minute edit rule vs. slow hands Mark...

Sorry about that. The formula would be: "< TODAY()+30"

Again, without the quote marks and you can delete the space between the less-than sign and the "T".
 
Upvote 0
Mark,
Thanks so much for the help. The longway (VBA) seems to work but not the conditional format.

This is what exactly I'm trying to do.
I have a vehicle list with verious service dates and would like the dates to show up in red when they are within 30 days.

Thanks,
Tmoske
 
Upvote 0
Mark,
Thanks so much for the help. The longway (VBA) seems to work but not the conditional format.

This is what exactly I'm trying to do.
I have a vehicle list with verious service dates and would like the dates to show up in red when they are within 30 days.

Thanks,
Tmoske


Happy to help :beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,974
Messages
6,182,100
Members
453,088
Latest member
Chaoxite

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