在 VBA 中比较日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20270928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare Dates in VBA
提问by loles
I have two textboxes with two differents dates, orderDate and recievedDate. The recieved date needs to be entered manually into the form and I want to include the validation that the recievedData should happen after the orderDate, I have tried:
我有两个具有两个不同日期的文本框,orderDate 和 recievedDate。收到的日期需要手动输入到表单中,我想包括 recievedData 应该在 orderDate 之后发生的验证,我试过:
If txtRecievedDate.Text < txtOrderDate.Text Then
MsgBox "Incorrect Date, item can't be recieved before order"
else
MsgBox "correct date"
End If
This doesn't work as for example RecievedDate value is "19/11/2013" and OrderDate value is "20/10/2013" although this would be a correct date this statement only compares the "19" and "20" therefore marking it as incorrect.
这不起作用,例如 RecievedDate 值为“19/11/2013”,OrderDate 值为“20/10/2013”,尽管这将是一个正确的日期,但此语句仅比较“19”和“20”,因此标记它不正确。
Is there a way of comparing two dates within textboxes? For this im using VBA
有没有办法比较文本框中的两个日期?为此,我使用 VBA
Thanks
谢谢
回答by Shiva
Here's how you can fix your code. Of course it's not the best way to do date calculations, and you'll also have to validate that there is text in the 2 text boxes, and maybe even use CDate()
to parse the text to date values, but this will work for your current situation.
以下是修复代码的方法。当然,这不是进行日期计算的最佳方法,您还必须验证 2 个文本框中是否有文本,甚至可能用于CDate()
将文本解析为日期值,但这适用于您当前的情况.
If DateDiff("d", txtOrderDate.Text, txtRecievedDate.Text) < 0 Then
MsgBox "Incorrect Date, item can't be recieved before order"
else
MsgBox "correct date"
End If
回答by jacouh
Apart from the excellent solution of Siddharth Rout at Formatting MM/DD/YYYY dates in textbox in VBA, I'll suggest the following procedure. UserForm is a natural object in Excel, but we shall make reference to an OCX object to use it in Access, so not directly available.
除了 Siddharth Rout 在Formatting MM/DD/YYYY 日期在 VBA 文本框中的出色解决方案之外,我将建议以下过程。UserForm 在 Excel 中是一个自然对象,但我们将引用一个 OCX 对象在 Access 中使用它,因此不能直接使用。
In Access with form object, one can do this:
在带有表单对象的 Access 中,可以这样做:
Sub sof20270928CompareDates()
If CDate(txtRecievedDate.Value) < CDate(txtOrderDate.Value) Then
MsgBox "Incorrect Date, item can't be recieved before order"
Else
MsgBox "correct date"
End If
End Sub
Instead of comparing text strings, we compare now dates. CDate() converts local date time strings into date type. An American will type the date in mm/dd/yyyy format, like 11/19/2013, in France one types dd/mm/yyyy, like 19/11/2013. CDate() will take care of this as Access considers the computer's locale.
我们不比较文本字符串,而是比较现在的日期。CDate() 将本地日期时间字符串转换为日期类型。美国人将以 mm/dd/yyyy 格式输入日期,例如 11/19/2013,在法国则以 dd/mm/yyyy 格式输入,例如 19/11/2013。CDate() 将处理此问题,因为 Access 会考虑计算机的区域设置。
Moreover, txtRecievedDate.Value is preferealbe to txtRecievedDate.Text, the latter requires focus, ie, the control must be active to get the .text property.
此外,txtRecievedDate.Value 比txtRecievedDate.Text 更受欢迎,后者需要焦点,即控件必须处于活动状态才能获得.text 属性。
回答by user3041384
try comparing them in date formats instead of in .text format
尝试以日期格式而不是 .text 格式比较它们
for example
例如
Sub datee()
Dim Orderdate As Date
Dim Recievedate As Date
Orderdate = Worksheets("Sheet1").Range("A1").Value
Recievedate = InputBox("Enter date recieved")
If Recievedate < Orderdate Then
MsgBox "error"
Exit Sub
Else
Worksheets("sheet1").Range("a3").Value = Recievedate
End If
End Sub