VBA highlight row w/ cell matching todays date

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
220
Office Version
  1. 365
Platform
  1. Windows
I'm always frustrated when I attempt to do what seems like a simple macro, and cannot accomplish it. Right now is one of those times.

After running a VBA based sort, I need my worksheet to highlight the rows whose cells in column Q match todays date (displayed as MM/DD).

Here's my code, which produces...nothing:
Code:
Dim cell As Range
lr = Cells(ActiveSheet.Rows.Count, 23).End(xlUp).Row
For Each cell In Range("Q2:Q" & lr)
        If cell.Value = Date Then
            cell.EntireRow.Interior.ColorIndex = 6
        Else
            cell.EntireRow.Interior.ColorIndex = xlNone
        End If
Next

Can someone kindly steer me in the right direction?

PS - conditional formatting is not the right solution for this application.

TIA
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this

Sub col2A()
Range("Q2").Select
Do Until ActiveCell = ""
ActiveCell.Offset(1).Select
If ActiveCell = Date Then ActiveCell.EntireRow.Interior.ColorIndex = 6

Loop
End Sub
 
Upvote 0
I think you have the wrong column!!!
Code:
Lr = Cells(ActiveSheet.Rows.Count, 17).End(xlUp).Row
 
Upvote 0
After running a VBA based sort, I need my worksheet to highlight the rows whose cells in column Q match todays date (displayed as MM/DD).

Here's my code, which produces...nothing:
Code:
Dim cell As Range
lr = Cells(ActiveSheet.Rows.Count, 23).End(xlUp).Row
For Each cell In Range("Q2:Q" & lr)
        If cell.Value = Date Then
            cell.EntireRow.Interior.ColorIndex = 6
        Else
            cell.EntireRow.Interior.ColorIndex = xlNone
        End If
Next

Can someone kindly steer me in the right direction?
Let's get an obvious question out of the way first... does Column W (what you are using to calculate the last row) contain data for at least as many rows as Column Q does? Also, if you are only checking Column Q for dates, why not use it to calculate the last row (or do you have other code coming later that may need the different last row the Column W provides)?
 
Upvote 0
Your code is actually fine, BUT if the date is not a valid date, (ie: a text date) is will not hilite.
Try this version

Code:
Dim cell As Range
Dim lr As Long
lr = Cells(ActiveSheet.Rows.Count, 23).End(xlUp).Row
For Each cell In Range("Q2:Q" & lr)
        If CDate(cell.Value) = Date Then
            cell.EntireRow.Interior.ColorIndex = 6
        Else
            cell.EntireRow.Interior.ColorIndex = xlNone
        End If
Next
 
Upvote 0
Your code is actually fine, BUT if the date is not a valid date, (ie: a text date)
The OP said "todays date (displayed as MM/DD)" which would imply to me it was a real date in which Cell Formatting was used to set the display in the cell, not a text one... or are you thinking the OP is using a formula like this in his cells =TEXT(NOW(),"MM/DD")?
 
Upvote 0
Nope; a moment of forgetfulness...
(I read it, but when I started thinking of a solution I slipped my mind.)

Well, there are a few replies to the OP's post, so lets see if there isn't a resolution within them...else more details will be needed.

Cheers Rick.
 
Upvote 0
I think you have the wrong column!!!
Code:
Lr = Cells(ActiveSheet.Rows.Count, 17).End(xlUp).Row

Hey everyone - thank you so much for the feedback. YES, this was the problem. I had this assigned to column W in an earlier part of the script for a different reason, and the data in that column ends...the day before today! No wonder!

Remember what I said about frustration when simple macros don't work? It seems to always be the most simple, obvious oversights that cause it!

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,244
Members
448,879
Latest member
VanGirl

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