我有一份更新表格。单击按钮时,会弹出“是”或“否”消息框,要求用户验证交货日期。
单击“否”后,将出现一个输入框以输入适当的日期。
我正在尝试验证用户是否输入了有效日期。在代码中,该函数从“Line1”开始。
问题出现在“Line1”下面的 Else 语句中。
如果我输入正确的日期,代码会跳到消息框,然后返回到“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