VBA highlight row w/ cell matching todays date

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
205
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,704
Office Version
  1. 2016
Platform
  1. Windows
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
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
I think you have the wrong column!!!
Code:
Lr = Cells(ActiveSheet.Rows.Count, 17).End(xlUp).Row
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,978
Office Version
  1. 2016
Platform
  1. Windows
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)?
 

RudiS

Active Member
Joined
May 7, 2015
Messages
349

ADVERTISEMENT

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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,978
Office Version
  1. 2016
Platform
  1. Windows
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")?
 

RudiS

Active Member
Joined
May 7, 2015
Messages
349
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.
 

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
205
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,811
Messages
5,638,488
Members
417,027
Latest member
wlknspc7

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
Top