Color cell when date is more than 40 days

Sha

New Member
Joined
Oct 6, 2021
Messages
30
Office Version
  1. 2013
Platform
  1. Windows
Hi

this is the code I have thus far.


Sub HighlightCells()

Dim dtrg As Range 'Date in Col J
Dim dtCell As Range 'Date Cell
Set dtrg = Range("J1:J3000") ' Date Range

For Each dtCell In dtrg.Cells
If dtCell.Value > dtCell.value + 40 Then
dtCell.Interior.ColorIndex = 3
End If
Next dtCell


End Sub
 
Last edited by a moderator:
the code is placed at the ever end of all the codes that I have. I only have 1 sheet.

VBA Code:
Public MAIL_WHO As String
Public XMAILBODY As String
Public SUBJECT_MESSAGE As String
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Column <> 16 And Target.Column <> 15 And Target.Column <> 14 Then Exit Sub
If Target.Value = "" Then Exit Sub


If Target.Column = 16 Then
    MAIL_WHO = "Linda@gmail.com"
    SUBJECT_MESSAGE = "Pending query processing" 'adds subject to email
   XMAILBODY = "Hi there" & vbNewLine & vbNewLine & _
        "Coding Query from: " & Range("E" & Target.Row).Value & vbNewLine & vbNewLine & _
        "Patient Name: " & Range("B" & Target.Row).Value & vbNewLine & vbNewLine & _
        "Case Number: " & Range("C" & Target.Row).Value & " (" & Range("D" & Target.Row).Value & ")" & vbNewLine & vbNewLine & _
        "Admission Date: " & Range("H" & Target.Row).Value & vbNewLine & vbNewLine & _
        " Discharge date: " & Range("J" & Target.Row).Value & vbNewLine & vbNewLine & _
        "Coding Query: " & Range("P" & Target.Row).Value & vbNewLine & vbNewLine & _
        "Thank you" & vbNewLine 'calling out and placing values of each col into email body
        
End If


If Target.Column = 15 Then
    If Target.Offset(0, -10) = "" Then Exit Sub
    If Target.Offset(0, -10) = "TL" Then MAIL_WHO = "Traves_lee@gmail.com"
    If Target.Offset(0, -10) = "KL" Then MAIL_WHO = "Kris_Loen@gmail.com"
    If Target.Offset(0, -10) = "RS" Then MAIL_WHO = "Rose@gmail.com"
    SUBJECT_MESSAGE = "Mental Incapacity updated" 'adds subject to email
    XMAILBODY = "Hi there" & vbNewLine & vbNewLine & _
        "Mental Incapacity updated as : " & Range("N" & Target.Row).Value & vbNewLine & _
        "Case Number: " & Range("C" & Target.Row).Value & " (" & Range("D" & Target.Row).Value & ")" & vbNewLine & "Thank you" 'calling out and placing values of each col into email body
End If

If Target.Column = 14 Then
    If Target.Offset(0, -9) = "" Then Exit Sub
    If Target.Offset(0, -9) = "TL" Then MAIL_WHO = "Traves_lee@gmail.com"
    If Target.Offset(0, -9) = "KL" Then MAIL_WHO = "Kris_Loen@gmail.com"
    If Target.Offset(0, -9) = "RS" Then MAIL_WHO = "Rose@gmail.com"
    'MAIL_WHO = "Linda@gmail.com"
    SUBJECT_MESSAGE = "Conditions Arises updated" 'adds subject to email
    XMAILBODY = "Hi there" & vbNewLine & vbNewLine & _
        "Conditions Arises updated : " & Range("N" & Target.Row).Value & vbNewLine & _
        "Case Number: " & Range("C" & Target.Row).Value & " (" & Range("D" & Target.Row).Value & ")" & vbNewLine & "Thank you" 'calling out and placing values of each col into email body
End If

 

Mail_small_Text_Outlook

End Sub


Sub Mail_small_Text_Outlook()

Dim xOutApp As Object
Dim xOutMail As Object

Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

On Error Resume Next
With xOutMail
    .To = MAIL_WHO
    .Subject = SUBJECT_MESSAGE
    .Body = XMAILBODY
    .Display 'or .send
End With
On Error GoTo 0

Set xOutMail = Nothing
Set xOutApp = Nothing

End Sub


Sub HighlightCells()

    Dim dtrg As Range: Set dtrg = Range("J1:J3000")
    Dim dtCell As Range

    For Each dtCell In dtrg.Cells
        If dtCell.Value <> "" And dtCell.Value < Date - 40 Then _
           dtCell.Interior.ColorIndex = 3
    Next dtCell

End Sub
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Please answer all the questions that I asked and try running the code by itself as you are not calling the code in your code that you posted (put the code in a regular module, click in the code and press F5)
 
Upvote 0
HI there,

I have managed to get the code to work! thank you so much!
 
Upvote 0
You're welcome (hopefully now you have tested the code separately you have put a line in the event code to call the sub).
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,532
Members
449,169
Latest member
mm424

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