[英]Trying to verify date is within data range
I am trying to ensure that my user enters a valid date both format wise and for the given data set that I have. 我试图确保我的用户输入一个有效的日期,既要使用格式,也要输入我拥有的给定数据集。 If the user enters an erroneous date, I want to prompt him or her to reenter a valid date.
如果用户输入了错误的日期,我想提示他或她重新输入一个有效的日期。 Then, once a valid date is entered I want to filter column A so just those dates show up.
然后,输入有效日期后,我要过滤A列,以便仅显示那些日期。
I am currently getting a 1004 error with the current code that I have. 我当前的当前代码出现1004错误。
Sub date_entered_exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(date_entered) Then
MsgBox "Input must be a date in the format: 'dd/mm/yyyy'"
Cancel = True
Else
date_entered = Format(date_entered, "dd/mm/yyyy")
If Not date_entered = Range("A").Find(what:=date_entered) Then
MsgBox "Input a date within the range"
Else
WholeSheetRange.AutoFilter Field:=1, Criteria1:="=date_entered"
End If
End If
End Sub
You most probably have declared date_entered as string and as a result of that you are searching this string within dates and you get nothing. 您很可能已将date_entered声明为字符串,因此,您正在日期内搜索此字符串,但您一无所获。
Try converting it to date with CDate: 尝试使用CDate将其转换为日期:
If Not CDate(date_entered) = Range("A").Find(what:=CDate(date_entered)) Then
EDIT: 编辑:
This a lot different than your code but it should give you an idea how you can achieve this: 这与您的代码有很大不同,但是它应该使您知道如何实现此目的:
Sub date_entered_exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Rng As Range
Dim date_entered As Date
'date_entered = "13/07/2017"
date_entered = Format(date_entered, "dd/mm/yyyy")
If Not IsDate(date_entered) Then
MsgBox "Input must be a date in the format: 'dd/mm/yyyy'"
Cancel = True
Exit Sub
End If
Set Rng = Range("A:A").Find(date_entered)
If Rng Is Nothing Then
MsgBox "Input a date within the range"
Else
WholeSheetRange.AutoFilter Field:=1, Criteria1:="=date_entered"
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.