【问题标题】:MsgBox+InputBox with Numbers,verify input number with IfMsgBox+InputBox with Numbers,用If验证输入的数字
【发布时间】:2017-09-11 20:46:32
【问题描述】:

新手提醒。我正在使用MsgBoxInputBox 组合,询问用户是否要删除具有指定值(输入框)的行(msgbox)。我循环它直到我在Msgbox 中得到一个vbNo,这似乎有效,但是 InputBox 用户值验证似乎不起作用。
我读过 Inputboxes 将值识别为字符串,所以不确定这是否是个问题?

我的查询

1) 我想限制用户只能在输入框中输入数字,数字也不能超过 4 位。我为此使用了 If 语句,但没有一个起作用。我正在考虑将 Inputbox Type 分配给 Integer,它将输入限制为数字,但如果用户输入错误并且并非所有人都知道他们做错了什么,则不会发出警告。

2) 当我将If UserValue = vbNullString Then GoTo ContinueIf 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    如果您不输入商店编号,我并不完全清楚确切的消息流应该是什么,但这样的事情应该可以简化事情:

    Dim UserValue             As Variant
    Dim UserReply             As Variant
    Do
        UserReply = MsgBox(Prompt:="Do you want any store transactions removed?", Buttons:=vbYesNo, Title:="Stores removed")
        If UserReply = vbYes Then
    
            UserValue = InputBox(Prompt:="Enter any store number you want removed from the file. Otherwise click Cancel", _
                                             Title:="CHOSE STORES TO RUN OR DELETE")
            If UserValue = vbNullString Then
                ' do nothing
            ElseIf Not IsNumeric(UserValue) Then
                    MsgBox "You must enter a numeric value"
            ElseIf Len(UserValue) > 4 Then
                    MsgBox "Max store number is 4 digits", vbCritical
            Else
                For r = 100000 To 1 Step -1
                    If Sheets(csv_sht).Cells(r, 1) = CLng(UserValue) Then Sheets(csv_sht).Rows(r).EntireRow.Delete
                Next r
            End If
        End If
    Loop Until UserReply = vbNo
    'Next step, continue as normal.
    

    【讨论】:

    • 如果您不输入商店编号或取消,或单击 MsgBox 上的否,它只会将您带到标签 Continue(在底部),该标签仅包含其余代码与此 Msgbox/InputBox 组合相关。非常感谢!我会尽快试试这个并告诉你
    • 谢谢你,Rory,它符合我的想法,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多