VBA - 从NOW函数中删除秒

时间:2016-02-04 20:57:48

标签: excel vba seconds

我有一些东西会在它发生前一小时通知我。为此,我在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

7 个答案:

答案 0 :(得分:4)

要从Now中删除秒数,您可以使用一些数学或来回文本转换。

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

这两个都返回一个真正的数字日期时间值,并且秒被剥离。他们没有将秒数平均到最近的分钟;只需删除它们。

答案 1 :(得分:4)

你可以将MyLimit绕到最近的一分钟:

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

在将其与单元格的内容进行比较时,请考虑您可能需要使用<=>=比较来避免问题,如果时间在&#34;错误&#34时发生变化;是平等的时候了。

答案 2 :(得分:2)

另一种方法是:

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

秒(现在)返回秒数,而/ 60/60/24将其转换为天数,每个日期和时间都存储在天中。使用此或Jeeped的答案,其中任何一个都应该有效。

修改

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

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

答案 3 :(得分:0)

答案 4 :(得分:0)

尝试限制=格式((现在),&#34; DD / MM / YYYY HH:MM&#34;)

答案 5 :(得分:0)

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

答案 6 :(得分:0)

每个VBA Office 2010及更高版本的替代方法:

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

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

https://msdn.microsoft.com/en-us/library/office/gg251759.aspx

的更多信息