VBA formatting based on dates

ffionnah

Board Regular
Joined
Jun 12, 2018
Messages
61
Hi,
Thank you, in advance, for any contributions to this code.

The code below isn't formatting. Essentially, what I need this code to do is start in column "D", row 3, and search for dates, highlighting any date that is within -4 to +2 days of a date listed in A1. I want this to then jump to column "G" and do the same thing, continuing this process for every 3 columns, until it reaches the last column, "Y". The goal is to have any date -4 to -1 be yellow, 0 would be green and +1 and +2 blue. The cells in column "A" would also be either green or blue, depending on the date.



Sub Datecolor()
Dim i As Long, j As Long, myDate As Date

myDate = Cells(1, 1)

For j = 4 To 25 Step 3

For i = 3 To 10000
If Cells(i, j) = myDate - 4 Then Cells(i & j).Interior.ColorIndex = 36 And Cells(i, 1).Interior.ColorIndex = 35
If Cells(i, j) = myDate - 3 Then Cells(i & j).Interior.ColorIndex = 36 And Cells(i, 1).Interior.ColorIndex = 35
If Cells(i, j) = myDate - 2 Then Cells(i & j).Interior.ColorIndex = 36 And Cells(i, 1).Interior.ColorIndex = 35
If Cells(i, j) = myDate - 1 Then Cells(i & j).Interior.ColorIndex = 36 And Cells(i, 1).Interior.ColorIndex = 35
If Cells(i, j) = myDate + 0 Then Cells(i & j).Interior.ColorIndex = 35 And Cells(i, 1).Interior.ColorIndex = 35
If Cells(i, j) = myDate + 1 Then Cells(i & j).Interior.ColorIndex = 37 And Cells(i, 1).Interior.ColorIndex = 37
If Cells(i, j) = myDate + 2 Then Cells(i & j).Interior.ColorIndex = 37 And Cells(i, 1).Interior.ColorIndex = 37
Next i
Next j

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Change "And" to ":" The way you have it, the And will give True or False so your statement is If(Cells(i,j)=Mydate then TRUE (or false)
 
Upvote 0
Thank you for the input. That did correct formatting for Column "A", however, the columns that house the dates are still not being formatted. These columns are D,G,J,M,P,S,V,Y.
 
Upvote 0
All your Cells(i & j) references need to be changed to Cells(i, j)
Oddly, you did that correctly in the IF part of your code, but not in the THEN part of it.
 
Upvote 0
You are welcome.

And welcome to the board!:)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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