如何要求用户只在用户窗体中输入一个五位数字?

How to demand from User to type a five digit number only in UserForm?

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

如果用户在 TextBox1(用户窗体)中输入一个五位数,该值将进入特定单元格中的 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

你应该考虑添加 "length of textbox1 does not equal 5 then..."

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

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