我有一个excel文件。
我希望编写一个Excel vba来比较系统时间和单元格值时间。
如果系统时间超过单元格值时间,它将显示弹出消息,通知用户时间超过。
我的文件将如下所示:
我已经研究了一段时间,但似乎只有vba代码才能完成此要求。
答案 0 :(得分:1)
Sub TimeCheck()
Dim ValueTime As Date
Dim SysTime As Date
SysTime = Now()
Finalrow = Cells(Rows.Count, 14).End(xlUp).Row
'Column 14 stands for N, change as required
For I = 6 To Finalrow
'6 stands for first row filled with value, change as required
ValueTime = Cells(I, 14).Value
If TimeValue(ValueTime) < TimeValue(SysTime) Then
Cells(I, 14).Offset(, 1).Value = "Time is exceeeded" '1 is offsetting to column O. Use 2 for column P, 3 for Q and so on, as you prefer.
MsgBox ("Time is exceeeded for user entry in N" & I)
'To store the time error in adjacent O column cells, and to popup for each error
'Remove either as required - esp MsgBox, it is very annoying - put only because you asked in original question
End If
Next I
End Sub
答案 1 :(得分:0)
如果您只想告知客人时间输入不超过当前值,您不需要vba(相交将是单向)您可以使用验证日期
如果值不正确,您可以自定义输入消息以及错误消息。
答案 2 :(得分:0)
实施例
Sub TimeNow()
Dim cValue As Date '// Cell Value
Dim sTime As Date '// System
cValue = Sheets("Sheet1").Range("B2").Value
sTime = TimeValue(Now)
If sTime > cValue Then
MsgBox "TiMe iS Up. STOP " & TimeValue(Now)
Else: Exit Sub
'or do something
End If
End Sub
答案 3 :(得分:0)
您可以使用函数TimeValue,它将时间值作为0到1之间的数字返回。发布一个简单的代码来单独检查单元格N6。
///当然,您可以使用循环来检查一系列单元格,或者使用excel事件或键盘快捷键来运行宏.///
Sub TimeCheck()
Dim ValueTime As Date
Dim SysTime As Date
ValueTime = Range("N6").Value
SysTime = Now()
If TimeValue(ValueTime) < TimeValue(SysTime) Then
MsgBox ("Time is exceeeded")
End If
End Sub