Color Fill a range of cells depending on a date that is 10 or more days earlier than current date using vba.

Patches01

New Member
Joined
Aug 11, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi All

I would like to fill a range of cells with color using a vba. The cells will be colored depending on the date. For example today's date is 27/08/2021 I need the cells in the row with a date that is 10 days or more earlier (17/08/2021) to be filled.
This is what I have so far however it seems to color every second row.
Any help or advise will be greatly appreciated.
Thank you

Private Sub CommandButton4_Click()

Dim TDateM As String
TDateM = Date

endrow = Range("a" & Rows.Count).End(xlUp).Row

For Each cell In Range("a1:a" & endrow)
If cell.Value < TDateM Then
cell.Range("$a$" & cell.Row & ":$m$" & cell.Row).Interior.ColorIndex = 6
End If
Next

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Is this what you want?
VBA Code:
Private Sub CommandButton4_Click()
Dim TDateM As Variant
TDateM = Date
endrow = Range("a" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("A1:M" & endrow).Interior.ColorIndex = xlNone
For Each cell In Range("a1:a" & endrow)
    If TDateM - cell.Value >= 10 Then
        cell.Resize(1, 13).Interior.ColorIndex = 6
    End If
Next cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thank you. Works perfectly.
Is this what you want?
VBA Code:
Private Sub CommandButton4_Click()
Dim TDateM As Variant
TDateM = Date
endrow = Range("a" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("A1:M" & endrow).Interior.ColorIndex = xlNone
For Each cell In Range("a1:a" & endrow)
    If TDateM - cell.Value >= 10 Then
        cell.Resize(1, 13).Interior.ColorIndex = 6
    End If
Next cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Switched the marked solution with the answer that works perfectly.

@Patches01: Please do the same for your future questions marking the answer post as the solution instead of your feedback post unless you post your own solution. Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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