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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Greetings Timoske,

This presumes nothing will be entered outside of A1:N40. Change to suite.

The below goes in the sheet's module, so right-click the sheet's tab, select View Code, and paste in VBIDE.

Hope this helps,

Mark

In the Worksheet Module:
Code:
Option Explicit

Private Sub Worksheet_Activate()
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&
        End If
    Next
        
End Sub
 
Upvote 0
Mark,
Thanks for the reply. I have issue with VBIDE. What is it? and what am I doing with it.

I am new to VBA and I haven't come across VBIDE yet.

Thanks,
Tmoske
 
Upvote 0
Why not just use conditional formatting?:unsure:
 
Upvote 0
...so right-click the sheet's tab, select View Code, and paste in VBIDE.

When you right-click the sheet's tab and select View Code, this takes you to VBIDE; that is, the code editing window.

For a simple test, open a new/blank workbook and place this code in the sheet's module:

Code:
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&
        End If
    Next
End Sub

Now when you go back to the sheet, try typing in some dates in any cell in the range of A1:N40. The font will change to red presuming the date is less than 30 days from now.

Using the change event (in this second example) might be better, as the font will change as soon as the date is entered (after the cell (ie edit mode) is exited), rather than waiting til the next time the sheet is activated.

A couple of "maybe's" to consider:
  • If you already know specific cells that dates will be entered in, you could specify to check just these cells.
  • You might want to also change the font back to black if a new date is entered that is over 30 days out.
Mark
 
Upvote 0
Norie,
How can I get the font of a cell to change to red if it is within 30 days of today?

I just thought a VBA code would be easier

Thanks,
Tmoske
 
Upvote 0
Hi Norie - Jeepers, I shoulda thought of that.:eek:


To use conditional formatting, change the "Cell Value Is" to "Formula Is" in the drop-down, and for the formula: ="< Today()+30"<TODAY()+30"< p>
Better?

Mark
 
Upvote 0
Thanks a lot!! Mark
That works great! Just what I needed. Thanks for the explanation.

Thanks so much,
Tmoske
 
Upvote 0
Just for my clarity, you settled on the conditional formatting, right? Just asking so as not to leave the font red if you used the code and then a "too close for comfort date" was overwritten with an acceptable date.

Happy to help :)

Mark
 
Upvote 0
Mark,
I tried that conditional format formula and can not get it to work.

="< Today()+30"<TODAY()+30"< p>
 
Upvote 0

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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