I've checked through multiple topics but I unfortunately couldn't find the solution I need.
I have cells which needs to include Date and Time in the following custom format: dd/mm/yyyy hh:mm
And I have something that check those cells to see if it matches. If it does, it notifies me that it is approaching its due time.
Dim MyLimit As Double
MyLimit = Date
Set FormulaRange = Range("E5:E35")
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If .Value = MyLimit Then
*Do the action*
This works properly if I set a date value such as 02/05/2016 in the cells, as it default to midnight and it triggers the action. Although 02/05/2016 2:45 PM doesn't work.
So what I need it to do is to check only the first part of the cell for the date value and trigger the action if it fits with today's date.
I tried a few solutions such as:
Dim MyLimit As Double
MyLimit = CDate(format(Date, "dd-mmm-yyyy"))
Set FormulaRange = Range("E5:E35")
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If .Value = MyLimit Then
*Do the action*
and
Dim MyLimit As Date
MyLimit = Format((Now), "DD/MM/YYYY")
Set FormulaRange = Range("E5:E35")
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If .Value = MyLimit Then
*Do the action*
But it doesn't work. It doesn't detect that it should do the action as the date = today.
Anyone can enlighten me on this?
Actual script:
Option Explicit
Public Function AutoRun()
Application.OnTime Now + TimeValue("00:01:00"), "TaskTracker2"
End Function
Public Sub TaskTracker2()
Dim FormulaCell As Range
Dim FormulaRange As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim SendTo As String
Dim CCTo As String
Dim BCCTo As String
Dim MyLimit As Double
Dim MyLimit2 As Double
NotSentMsg = "Not Sent"
SentMsg = "Sent"
SendTo = Range("D2")
CCTo = Range("E2")
BCCTo = Range("F2")
MyLimit = Date
MyLimit2 = ((Round(Now * 1440, 0) - 30) / 1440)
Set FormulaRange = Range("E5:E35")
On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If DateValue(.Value) = MyLimit Then
MyMsg = SentMsg
If .Offset(0, 1).Value = NotSentMsg Then
strTO = SendTo
strCC = CCTo
strBCC = BCCTo
strSub = "[Task Manager] Reminder that you need to: " & Cells(FormulaCell.Row, "A").Value
strBody = "Hello Sir, " & vbNewLine & vbNewLine & _
"This email is to notify that you that your task : " & Cells(FormulaCell.Row, "A").Value & " with the following note: " & Cells(FormulaCell.Row, "B").Value & " is nearing its Due Date." & vbNewLine & "It would be wise to complete this task before it expires!" & _
vbNewLine & vbNewLine & "Truly yours," & vbNewLine & "Task Manager"
If sendMail(strTO, strSub, strBody, strCC) = True Then MyMsg = SentMsg
End If
Else
MyMsg = NotSentMsg
End If
If .Value = MyLimit2 Then
MyMsg = NotSentMsg
End If
Application.EnableEvents = False
.Offset(0, 1).Value = MyMsg
Application.EnableEvents = True
End With
Next FormulaCell
AutoRun
ExitMacro:
Exit Sub
EndMacro:
Application.EnableEvents = True
MsgBox "Some Error occurred." _
& vbLf & Err.Number _
& vbLf & Err.Description
End Sub
So what I need it to do is to check only the first part of the cell for the date value
Use the DateValue
function, eg:
If DateValue(.Value) = MyLimit Then
' *Do the action*
NB: a Date
type consists of Date and Time parts.
The DateValue
function returns only the Date Part of a Date
type. There is also the TimeValue
function which returns only the Time Part
from a Date
type.
Some revisions for your Mismatch error, tested with the value 2/5/2016 14:45
:
Dim MyLimit As Date
MyLimit = Date
Set FormulaRange = Range("E5:E35")
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If DateValue(.Value) = MyLimit Then
' Do something...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.