【发布时间】:2015-06-08 19:18:34
【问题描述】:
我有一个用于我正在制作的数据库数据提取器的用户窗体。其中有一个文本框,用于输入用户想要提取数据的部件号。我想在大部分提取器运行之前验证用户是否输入了正确的部件号格式。为此,我需要一个代码来验证文本是否以特定格式输入:
3 个数字字符 1 个字母字符或 1 个连字符 然后是 5 个数字字符
我一开始尝试了以下验证:
'validate that box is not empty
If TextBox1.Value = "" Then
MsgBox ("Sorry, you need to provide an Amount")
TextBox1.SetFocus
Exit Sub
End If
'validate that box is numeric
If Not IsNumeric(TextBox1.Value) Then
MsgBox ("Sorry, must enter a numer")
TextBox1.SetFocus
Exit Sub
End If
但后来我意识到我的问题是第四个位置可能有字母字符或连字符。
如果有任何建议,我将不胜感激。
提前致谢。
【问题讨论】:
-
到目前为止你尝试过什么?无论如何,请看这里以获取某些位置的字符:stackoverflow.com/questions/17127272/… 然后您可以测试字符,或者您可以提取子字符串并分析它们(
IsNumeric()等 ...) -
@smagnan 我已经有以下内容:'验证该框不为空 If TextBox1.Value = "" Then MsgBox ("Sorry, you need an Amount") TextBox1.SetFocus Exit Sub End If 'validate that box is numeric If Not IsNumeric(TextBox1.Value) Then MsgBox ("Sorry, must enter a numer") TextBox1.SetFocus Exit Sub End If 但是我意识到我必须考虑字母或连字符排在第 4 位,所以我需要更改代码,但我是新手,所以不确定如何操作,不过我可以使用 get-char 方法,所以谢谢
标签: validation excel textbox userform vba