Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-从现在起删除秒数功能_Excel_Vba_Seconds - Fatal编程技术网

Excel VBA-从现在起删除秒数功能

Excel VBA-从现在起删除秒数功能,excel,vba,seconds,Excel,Vba,Seconds,我有件事在事情发生前一小时通知我。为此,我在VBA中使用NOW函数,因为我需要它来检查日期 问题是脚本每隔20秒运行一次,所以我不能让它考虑现在函数的秒。< /P> 有没有办法去掉这些?只喜欢(天、月、年、小时、分钟) 大致如下: MyLimit = NOW(DAY,MONTH,YEAR,HOUR,MINUTE) For Each FormulaCell In FormulaRange.Cells With FormulaCell If .Value = MyLi

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

问题是脚本每隔20秒运行一次,所以我不能让它考虑现在函数的秒。< /P> 有没有办法去掉这些?只喜欢(天、月、年、小时、分钟)

大致如下:

 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函数


更新

尝试使用limit=Format((现在),“DD/MM/YYYY HH:MM”)

要将秒数从中去掉,您可以使用一些数学或文本转换

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

这两个函数都返回一个真实的数字datetime值,去掉秒数。它们不会将秒数平均到最接近的分钟数;只需移除它们。

另一种方法是:

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

编辑:

为避免微小但存在的错误,请使用以下方法:

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

你可以把我的限制缩短到最近的一分钟:

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

考虑一下,在将其与单元格内容进行比较时,如果相等值保持为true,时间在“错误”时间发生变化时,可能需要使用
=
比较来避免出现问题。

我通常只需使用函数
=时间(小时(现在())、分钟(现在()),0)

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

 Dim DateWithoutSeconds : DateWithoutSeconds = DateAdd("s",-Second(Now),Now)
注意减号(-)会删除秒


时,它无法检查当天的时间:/因此,如果我使用日期,并且参考单元格是根据SAYSOR 02/04/2016-4:02 PM,则我将无任何结果。但是,如果只使用2016年4月2日的日期,它确实可以工作。那么格式(NOW(),“dd/mm/yyyy-hh:mm AM/PM”)呢?当我尝试MyLimit=Format((NOW),“dd/mm/yyyyy hh:mm”)时,我会遇到一个运行时错误“13”-类型不匹配。此外,如果我删除MyLimit并执行以下操作,脚本不会触发:if.Value=Format((NOW),“dd/mm/yyyyyyy hh:mm”),我小心地将范围内的单元格格式化为自定义格式DD/MM/YYYY HH:MMI在尝试MyLimit=format((现在),“DD/MM/YYYY HH:MM”)时遇到运行时错误“13”-类型不匹配。我相信CLng会舍入(并舍入到最近的偶数)您确实正确;我一定是混淆了我尝试过的几种方法的结果。非常感谢Jeeped,看起来这也很有效:)我将使用Ron的解决方案,因为他发布得更快,他的作品也很棒!非常感谢@弗朗西斯·阿尔塔斯——不用担心;这里有很多好主意。然而,我会质疑你对“更快”的定义,因为我的帖子比罗恩的早了七分钟。哦,那是真的,我误读了《泰晤士报》。我的坏D:我能选两个最好的答案吗?那真是太神奇了!谢谢,我经历了这么多的可能性!如果我想在30分钟前收到通知,我会怎么做?我不太擅长编写函数的多个修改:P@FrancisMaltais在进行比较之前,只需在MyLimit中添加30分钟。30分钟=30/1440或
Timeserial(0,30,0)
所以类似于:MyLimit=(Round(现在是*1440,0)/1440)-30/1440?@FrancisMaltais您可以,不过为了清楚起见,我会使用两行;第一个是对我的限制进行四舍五入,第二个是对30分钟进行加减。谢谢:)看起来也可以!非常感谢。