Change color of cell when dates match (4 conditions)

ceslyn

New Member
Joined
Mar 4, 2009
Messages
48
Anyone can tell me how to make this work?
I'm always getting the amount of 40611 and my cells are not changing.

Any help would be appreciated.

Thanks!


Sub findate()
Dim strDate1, strDate2 As Date
strDate1 = myplage
strDate2 = Now
testdates = DateDiff("d", strDate1, strDate2)
msgbox (testdates)
Set myplage = Range("B6:N18")
For Each cell In myplage
testdates = DateDiff("d", strDate1, strDate2)
If testdates > 5 Then Range("b6").Interior.Color = RGB(200, 160, 35)
If testdates < 5 Then Range("b6").Interior.Color = RGB(255, 255, 255)
If testdates = 5 Then Range("b6").Interior.Color = RGB(50, 50, 50)
Next
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
myplage is actually the range of cells that it's going thru.

I've managed to make it work but as soon as enter the loop, i'm always getting the same number.
 
Upvote 0
Try (untested):

Code:
Sub findate()
    Dim strDate1, strDate2 As Date
    strDate2 = Now
    Set myplage = Range("B6:N18")
    For Each cell In myplage
        strDate1 = cell.Value
        testdates = DateDiff("d", strDate1, strDate2)
        If testdates > 5 Then Range("b6").Interior.Color = RGB(200, 160, 35)
        If testdates < 5 Then Range("b6").Interior.Color = RGB(255, 255, 255)
        If testdates = 5 Then Range("b6").Interior.Color = RGB(50, 50, 50)
    Next
End Sub
 
Upvote 0
Might be completely wrong - anything to do with strDate1 being a Variant, and strDate2 being a date?
 
Upvote 0
No all cells contain a date.
I've tried putting dates everywhere but it's only changing the color of the first cell.
 
Upvote 0
Can anyone explain to me why I'm getting a value of 40611.
Here is another code i'm trying.

Sub CellsExample()
Dim Startdate As Date
Dim Enddate As Date

strDate2 = Now
Enddate = Now

For i = 6 To 8
For j = 2 To 4
If Cells(i, j).Value <> "" Then
Enddate = Cells(i, j).Value

testdates = DateDiff("d", Startdate, Enddate)
If testdates > 39881 Then Cells(i, j).Interior.Color = RGB(200, 160, 35)
If testdates > 40661 Then Cells(i, j).Interior.Color = RGB(50, 50, 50)
If testdates < 398815 Then Cells(i, j).Interior.Color = RGB(255, 255, 255)
msgbox (testdates)
Else
End If

Next j
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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