VBA - 从 NOW 函数中删除秒数

VBA - Remove Seconds from NOW function

我有事情会在事情发生前一小时通知我。为此,我在 VBA 中使用了 NOW 函数,因为我也需要它来检查日期。

问题是脚本每 20 秒运行一次,所以我不能让它考虑 NOW 函数的秒数。

有没有办法删除那些?只喜欢 (DAY,MONTH,YEAR,HOUR,MINUTE)?

类似的东西:

 MyLimit = NOW(DAY,MONTH,YEAR,HOUR,MINUTE)

 For Each FormulaCell In FormulaRange.Cells
 With FormulaCell
            If .Value = MyLimit Then
            Call Notify

这是我尝试检测日期和时间的脚本。

Option Explicit

Public Function AutoRun()
Application.OnTime Now + TimeValue("00:00:20"), "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 Date


NotSentMsg = "Not Sent"
SentMsg = "Sent"
SendTo = Range("D2")
CCTo = Range("E2")
BCCTo = Range("F2")

MyLimit = Format((Now), "DD/MM/YYYY HH:MM")

Set FormulaRange = Range("E5:E35")
On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange.Cells
    With FormulaCell
            If .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
        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

使用 Date 函数而不是 NOW 函数 https://msdn.microsoft.com/en-us/library/aa227520(v=VS.60).aspx

更新

尝试 limit = Format((Now), "DD/MM/YYYY HH:MM")

要去除秒数 Now,您可以使用一些数学运算或文本转换。

CDate(format(Now, "dd-mmm-yyyy hh:mm"))
'... or,
CLng(Now * 1440)/1440

这两个 return 一个真正的数字日期时间值,去掉了秒数。他们不会将秒平均到最近的分钟;只需删除它们。

另一种方法是这样的:

MyLimit = now-second(now)/60/60/24

second(now) returns 秒数,/60/60/24 将其转换为天数,每个日期和时间都存储在天数中。使用这个或 Jeeped 的答案,其中任何一个都应该工作。

编辑:

为避免微小但存在的错误可能性,请使用:

MyLimit = now
MyLimit =MyLimit -second(MyLimit)/60/60/24

您可以将 MyLimit 四舍五入到最接近的分钟:

MyLimit = Round(Now * 1440, 0) / 1440

请考虑,将其与单元格的内容进行比较时,您可能需要使用 <=>= 比较,以避免时间在 "wrong" 发生变化时出现问题等式成立的时间到了。

我通常只使用函数=TIME(HOUR(NOW()),MINUTE(NOW()),0)

根据 VBA Office 2010 及更高版本的替代方法:

 Dim DateWithoutSeconds : DateWithoutSeconds = DateAdd("s",-Second(Now),Now)

请注意减号 (-) 会删除秒数。

更多信息请见https://msdn.microsoft.com/en-us/library/office/gg251759.aspx