VBA: Highlight Row if Date in that Row is < than a Input Date in another cell

Niburt

New Member
Joined
Nov 30, 2016
Messages
5
Hello, I'm a beginner in VBA and I trying to automate the flowing task:
I have a WS2 that represents my Checklist. On that Checklist I have Dates in Column P. The Dates tell us when check items were added and by doing that we can simply skip old one that are older than a specific date. I would like to have a Revision Date in Range("C6"), so that VBA can go thru my entire WB and if it finds in Column P a date that is older than my Revison Date in C6, it should Highlight/Grey out the Column(A:P) on that specific row.
Since our checklist is expanding, i think it will have to loop thru the entire WB. I know how to set it up in Conditional Formatting but is will not support it, if I add a row somewhere..

This is just a part of test code I put together and I'm missing the main part of it.

Code:
Dim row As Long
Dim lastrow As Long


lastrow = "All the day down"
col1 = 16
For row = 1 To lastrow


If Cells(row, col1).Value < WS2.Range("C6") Then "Highlight Colum(A:P) in that Row".Interior.Color = RGB(242, 242, 242)
End If
Next row

I appreciate any help!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to the Board!

What exactly is WS2?
Is it a Worksheet object you have defined elsewhere in your code?
Is it the name of a sheet, named range, or table?
 
Upvote 0
That's correct, the WS2 is defined as Worksheet2 and WB as Workbook and is part of my code.Sorry for not including it.
I actually managed to make it work using only conditional formatting, but I don't think its faster than VBA on large files.

Last minute I had the idea to let VBA loop thru the entire Workbook instead of just one Worksheet. So that if I ever add new Worksheets, I won't need to establish conditional formatting for each Worksheet.

I don't expect anyone to do the job for me, just help/guide me in the right direction. I think I'm close yet I don't know how to write my thoughts out in VBA language :LOL:
 
Upvote 0
Conditional Formatting should be faster than VBA. VBA with loops is notoriously inefficient and slow. Loops can be useful/helpful at times, but generally should be avoided when there are other alternatives.
Looping through your sheets wouldn't be as bad as looping through all the rows on each sheet.
So if you are looping to use VBA, it might be better to have VBA set up Conditional Formatting on each sheet, as it gets added.

By the way, the line of code you would need to replace the following in your original post:
Code:
If Cells(row, col1).Value < WS2.Range("C6") Then "Highlight Colum(A:P) in that Row".Interior.Color = RGB(242, 242, 242)
would be:
Code:
If Cells(myRow, col1).Value < WS2.Range("C6") Then Range(Cells(myRow,"A"), Cells(myRow,"P")).Interior.Color = RGB(242, 242, 242)

Note that I changed your row variable to myRow. You should never used reserved words, such as the names of existing functions or properties as variable names.
Excel and VBA sometimes cannot decipher if you are referring to the variable or the function/property, which can lead to errors and/or unexpected results.
 
Upvote 0
You Sir are a genius!! Thank you so much!

Not only did you teach me how to reach my goal, you also showed me the concerns about not properly naming my variables.
Two lessons learned.

You have a great day!
 
Upvote 0
You are welcome!
Glad I was able to help!:)
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,916
Members
448,533
Latest member
thietbibeboiwasaco

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