Change color of cell using VBA FOR Loop

ndjustin20

Board Regular
Joined
May 25, 2011
Messages
69
Hello,

I am trying to change the color of a cell, in a certain column, if the cells date is within 60 days of the date in the cell using VBA. I have figured out a way to do this using conditional formating though the problem I am running into is then filtering that specific color of cell as VBA doesn't recognize the conditional format. Here is what I have for a loop but it isn't working...

Code:
Sub seeIfDateIsWithin60Days()
Dim dateColE As Range
For Each dateColE In Range("E7:E125")
If dateColE - Now() <= 60 Then
dateColE.Interior.ColorIndex = 3
End If
Next dateColE
End Sub

Any and all help is greatly appreciated :)


Justin
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Just to confirm, when you're comparing dataColE - Now() <= 60.
You're seeing if they are less than 60 days apart.
It could be that orrrr

you could use this method
Code:
Dim i%
For i = 7 To 125
    If Range("E" & i).Value - Now() <= 60 Then
        Range("E" & i).Interior.ColorIndex = 3
    End If
Next i

using loop like this is usually faster than For Each loops.

and make sure your column E7:E125 are formatted as time. (or... date..I dunno lol)
 
Upvote 0
I am getting a type mismatch error using the following code...


Code:
Sub findWithin60Days()
Dim i%
For i = 7 To 125
    If Range("E" & i).Value - Now() <= 60 Then
        Range("E" & i).Interior.ColorIndex = 3
    End If
Next i
End Sub
 
Upvote 0
What are you trying to subtract against?
Now() returns the date and the time fyi.

So, what is in your E7:E125?
 
Upvote 0
Dates are in Column E. So I am subtracting the date in column E from todays date to see if it is <= 60 days so I know if I need to renew that account which works though I can't figure out how to make it run in VB code. I can get the following to run using conditional formatting...


=AND(A1<>"",A1-TODAY()<=30)
 
Upvote 0
Try
Code:
Sub findWithin60Days()
Dim i%
For i = 7 To 125
    If Range("E" & i).Value <> "" Then
        If Range("E" & i).Value - Date() <= 60 Then
            Range("E" & i).Interior.ColorIndex = 3
        End If
    End If
Next i
End Sub
 
Upvote 0
Below is the line of code that is throwing the type mismatch error "13"

Code:
Sub findWithin60Days()
Dim i%
For i = 7 To 125
    If Range("E" & i).Value <> "" Then
   [COLOR=red] If Range("E" & i).Value - Date() <= 60 Then
[/COLOR]        Range("E" & i).Interior.ColorIndex = 3
    End If
    End If
Next i
End Sub
 
Upvote 0
Please tell me what your E7:E125 cell format is in.

And an example of how they look like! (ex. 2/5/2011, mm/dd/yyyy)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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