Highlighting range when single cell in range is certain date with vba

L

Legacy 259939

Guest
I have a spreadhseet that I update and copy from multiple times a day. I am trying to automate as much formatting as possible with vba.

I want to highlight a#,j# if c# is a certain date. I have a rough idea of the formula for choosing date it is the code I am having trouble with. I found code to highlight the single cell but I want to highlight the a - j of that row, not the entire row.

I have 3 sets of dates I am looking for:
if c#=today then a#,j#.interior=yellow
if c# <today and >=(today-7) then a#,j#.interior=light red
if c# <(today-7) then a#,j#.interior=dark red with white text

I know conditional formatting will do this but when the data is copied and pasted somewhere else the formatting does not come with it.

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I assume that by
c# <today and="">=(today-7) then a#,j#.interior=light red
</today>
you mean a date between today and seven days ago. Try this macro. It applies only to row 1.
Code:
Sub FillRange()
    If Range("C1") = Date Then
        Range("A1:J1").Interior.ColorIndex = 6
        Range("A1:J1").Font.Color = vbBlack
    ElseIf Range("C1") >= Date - 7 And Range("C1") <= Date Then
        Range("A1:J1").Interior.ColorIndex = 3
        Range("A1:J1").Font.Color = vbBlack
    ElseIf Range("C1") < Date - 7 Then
        Range("A1:J1").Interior.ColorIndex = 3
        Range("A1:J1").Font.Color = vbWhite
    End If
End Sub
 
Upvote 0
That did work. :) Now how do I get it do go down and do the same for each row that has data?
 
Upvote 0
Try:
Code:
Sub FillRange()
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim bottomC As Long
    bottomC = Range("C" & Rows.Count).End(xlUp).Row
    For Each rng In Range("C2:C" & bottomC)
        If rng = Date Then
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Interior.ColorIndex = 6
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Font.Color = vbBlack
        ElseIf rng >= Date - 7 And Range("C1") <= Date Then
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Interior.ColorIndex = 22
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Font.Color = vbBlack
        ElseIf rng < Date - 7 Then
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Interior.ColorIndex = 3
            Range(Cells(rng.Row, 1), Cells(rng.Row, 10)).Font.Color = vbWhite
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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