Highlight entire row if date exceeded 3 days

coolman

New Member
Joined
Nov 21, 2017
Messages
36
Hi there

I am trying to use vba to check each cells in the column if the date has exceeded 3 days. If so the whole row will be highlighted red.

Here is my syntax

If Range("a:a").value < Range("a:a").value + 3 then

Range("a:a")EntireRow.Interior.ColorIndex = 8

End If

However I could not get the program to work. Are there anyone who can advice me on this?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
try this
Code:
Sub t()
Range("A:A").AutoFilter 1, "<" & Date - 3
Range("A2", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Interior.Color = vbYellow
ActiveSheet.AutoFilterMode = False
End Sub
 
Last edited:
Upvote 0
try this
Code:
Sub t()
Range("A:A").AutoFilter 1, "<" & Date - 3
Range("A2", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Interior.Color = vbYellow
ActiveSheet.AutoFilterMode = False
End Sub
I would point out that if no dates in Column A meet the required date criteria, then the first row will become colored yellow with your code.
 
Last edited:
Upvote 0
Is there any way to prevent the first row to become yellow ? Also would it be possible to have a simpler syntax to perform the comparison as I am at the beginner level
 
Upvote 0
Hi
There you go... ;)

Try this..
Code:
' Tested and working..

Sub changeCol()


Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
If Range("A" & i).Value <> "" Then
If Range("A" & i).Value < Date - 3 Then
Range("A" & i).EntireRow.Interior.Color = vbYellow
Else:
Range("A" & i).EntireRow.Interior.Color = vbWhite
End If
End If
Next i


End Sub
 
Last edited:
Upvote 0
Is there any way to prevent the first row to become yellow ? Also would it be possible to have a simpler syntax to perform the comparison as I am at the beginner level


Hi , Try my code above mate, it works as you desire..

:D
 
Upvote 0
Hi there

Thanks for the syntax.

I would like to clarify this syntax line:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

what is syntax trying to perform?

Also for this syntax: If Range("A" & i).Value < Date - 3, if i change it to weekdays, do i have to use dateadd to perform it?

Hi
There you go... ;)

Try this..
Code:
' Tested and working..

Sub changeCol()


Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
If Range("A" & i).Value <> "" Then
If Range("A" & i).Value < Date - 3 Then
Range("A" & i).EntireRow.Interior.Color = vbYellow
Else:
Range("A" & i).EntireRow.Interior.Color = vbWhite
End If
End If
Next i


End Sub
 
Upvote 0
Hi there

Thanks for the syntax.

I would like to clarify this syntax line:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

what is syntax trying to perform?

Also for this syntax: If Range("A" & i).Value < Date - 3, if i change it to weekdays, do i have to use dateadd to perform it?



1). It would pull last used row number.
2). I tried with weekday function but it didn't work.
 
Upvote 0
I would point out that if no dates in Column A meet the required date criteria, then the first row will become colored yellow with your code.
this shoud fix that
Code:
Sub t()
Range("A:A").AutoFilter 1, "<" & Date - 3
If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count = 1 Then
    Range("A2", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).EntireRow.Interior.Color = vbYellow
End If
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
For this syntax line: If Range("A" & i).Value <> "" Then

It produce an error 440 message when i tried to run the program.

Is there any way to resolve the error?


1). It would pull last used row number.
2). I tried with weekday function but it didn't work.
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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