在我的项目中,我有一个更新表单。单击更新按钮时,会弹出一个消息框,要求用户验证交付日期。这是一个是或否消息框。单击“否”后,会出现一个输入框,允许用户输入适当的日期。
我遇到的问题是用户输入日期后的数据验证。我正在尝试验证用户是否输入了有效日期,而不是一串字母字符。在代码中,此功能从“Line1”开始。问题出现在“Line1”下方的 Else 语句中。如果我输入格式正确的日期,代码会跳到 Msgbox,然后返回到“Line1”。如果我输入一串字母字符,我会收到运行时错误 13 类型不匹配,而不是提示我更正日期的消息框。
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
iRow = ActiveCell.Row
lastRow = ws3.Cells(ws3.Rows.Count, 1).End(xlUp).Row
For i = 3 To lastRow 'Isolate the Record, Get data, and move
wo = Cells(i, 1).Value
pn = Cells(i, 2).Value
sn = Cells(i, 3).Value
n = Cells(i, 6).Value
If Me.txt_WN.Value = wo Then
If Me.txt_pn.Value = pn Then
If Me.txt_sn.Value = sn Then
If n = "Yes" Then
dd = MsgBox("Is this the correct delivery date? " & Curr, vbYesNo)
If dd = vbYes Then
dd = Curr
Else
Line1: 'Problem exists here
If IsDate(dd = InputBox("Enter the Correct Delivery Date", "Deliver to Stores Date")) Then
dd = Format(dd, "mm/dd/yyyy")
Else
MsgBox "The date is formatted incorrectly, please recheck entry"
GoTo Line1
End If
'If IsDate(dd) Then
' dd = Format(dd, "mm/dd/yyyy")
'Else
' MsgBox "The date is formatted incorrectly, please correct"
' GoTo Line1
'End If
End If
GoTo Update
Else
MsgBox "This Wheel S/N was not marked as Due for NDT"
Exit Sub
End If
End If
End If
End If
Next i
在“Line1”之后的第一个 If 语句下方,您将看到第二个 if 语句已被注释掉。在这种情况下,如果我单击消息框上的“否”按钮,它将输入“1/7/1900”,并基本上绕过验证代码(我认为)。
我不确定发生了什么。我在其他子例程中使用了类似的验证代码,它只是找到了。我不确定我的错误在哪里,非常感谢任何建议。
通过使用工作表引用限定
Cells()
来避免潜在问题,否则它默认为活动工作表。
With ws3
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow 'Isolate the Record, Get data, and move
wo = .Cells(i, 1).Value
pn = .Cells(i, 2).Value
sn = .Cells(i, 3).Value
n = .Cells(i, 6).Value
If Me.txt_WN.Value = wo _
And Me.txt_pn.Value = pn _
And Me.txt_sn.Value = sn Then
If n = "Yes" Then
If vbYes = MsgBox("Is this the correct Delivery Date? " & curr, vbYesNo) Then
dd = curr
Else
Line1: dd = InputBox("Enter the correct Delivery Date", "Deliver to Stores Date")
If Len(dd) = 0 Then ' cancel
Exit Sub
ElseIf IsDate(dd) Then
dd = Format(dd, "mm/dd/yyyy")
Else
MsgBox "'" & dd & "' is not a valid date, please recheck entry", vbExclamation
GoTo Line1
End If
End If
GoTo update
Else
MsgBox "This Wheel S/N " & sn & " was not marked as due for NDT", vbCritical
Exit Sub
End If
End If
Next i
End With