【问题标题】:How to demand from User to type a five digit number only in UserForm?如何要求用户只在用户窗体中输入一个五位数字?
【发布时间】:2020-06-15 18:46:19
【问题描述】:

我有代码需要用户填写数据,并且数据应该只有一个五位数,或者用户应该得到一个 msgBox“只能是数字”或类似的东西。

如果用户在 TextBox1 (UserForm) 中输入一个五位数字,则该值将转到特定单元格中的 Excel 工作表。这就是代码要做的想法。

我的代码正在检查用户是否在单元格中输入数字,但不要求输入格式,例如“00000”。

用户可以输入一个三位数字,代码将运行。

Private Sub TextBox1_Change()

If Not IsNumeric(TextBox1.Value) = Format(TextBox1.Value, "") = True Or TextBox1.Value = vbNullString Then
    MsgBox "Invalid data type"

Else: TextBox1.Value = Format(TextBox1.Value, "")
    Sheets("POVRATNICE").Range("B2").Value = TextBox1.Value
    
End If

End Sub

【问题讨论】:

    标签: excel vba format user-input userform


    【解决方案1】:

    您应该考虑添加“textbox1 的长度不等于 5 则...”

    If Not IsNumeric(TextBox1.Value) = Format(TextBox1.Value, "") = True Or TextBox1.Value = vbNullString Or len(TextBox1.value) <> 5 Then
    

    如果文本框不是 5 个字符,那么您会收到消息...

    【讨论】:

    • 这将在他们每次输入字符时触发,因此前 4 个字符会在他们尝试输入 5 时弹出一个消息框。
    • 那行。非常感谢!所以这是最终代码: Private Sub TextBox1_Change() If Not IsNumeric(TextBox1.Value) = Format(TextBox1.Value, "") = True Or TextBox1.Value = vbNullString Or Len(TextBox1.Value) 5 Then Else: TextBox1.Value = Format(TextBox1.Value, "") Sheets("POVRATNICE").Range("B2").Value = TextBox1.Value End If
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多