【发布时间】:2017-09-11 20:46:32
【问题描述】:
新手提醒。我正在使用MsgBox 和InputBox 组合,询问用户是否要删除具有指定值(输入框)的行(msgbox)。我循环它直到我在Msgbox 中得到一个vbNo,这似乎有效,但是 InputBox 用户值验证似乎不起作用。
我读过 Inputboxes 将值识别为字符串,所以不确定这是否是个问题?
我的查询
1) 我想限制用户只能在输入框中输入数字,数字也不能超过 4 位。我为此使用了 If 语句,但没有一个起作用。我正在考虑将 Inputbox Type 分配给 Integer,它将输入限制为数字,但如果用户输入错误并且并非所有人都知道他们做错了什么,则不会发出警告。
2) 当我将If UserValue = vbNullString Then GoTo Continue 与If UserValue <> vbNullString Then GoTo StoreEntered 相反的行放入时 - 程序不起作用,有区别吗?任何建议都非常感谢。
3) UPD 我刚刚尝试点击vbYes on Mgsbox,然后点击Cancel on InputBox,我发誓它以前有效,但似乎不再有效。我原以为引用vbNullString 可以解决不输入任何内容并单击“确定”或只是取消输入框的问题。
这是我的代码
Dim UserValue As Integer
Dim UserReply As Variant
KeepLoop:
Do
UserReply = MsgBox(Prompt:="Do you want any store transactions removed?", Buttons:=vbYesNo, Title:="Stores removed")
If UserReply = vbNo Then GoTo Continue
If UserReply = vbYes Then GoTo UserInputReqd
Loop Until UserReply = vbNo
UserInputReqd:
UserValue = Application.InputBox(Prompt:="Enter any store number you want removed from the file. Otherwise click Cancel", _
Title:="CHOSE STORES TO RUN OR DELETE") ', Type:=1)
If UserValue <> vbNullString Then GoTo StoreEntered 'If user left line blank and clicked 'Ok' or clicked 'Cancel'
If Not IsNumeric(UserValue) Then
MsgBox "You must enter a numeric value"
If Len(UserValue) > 4 Then
MsgBox "Max store number is 4 digits", vbCritical
End If
End If
StoreEntered:
r = 100000
Do
If Sheets(csv_sht).Cells(r, 1) = UserValue Then Sheets(csv_sht).Rows(r).EntireRow.Delete
r = r - 1
Loop Until r = 1
GoTo KeepLoop
Continue: 'Next step, continue as normal.
==Code here==
End Sub
【问题讨论】: