验证用户输入 [英] Validate User Entry
问题描述
我正在尝试运行一个marco,以检查用户输入的单元格并确保它们不是空白.我在一个牢房里很难过.我希望用户只能输入2个字母,并且希望它检查以确保没有在该单元格中输入任何数字,否则抛出错误消息并退出sub.任何帮助,我们将不胜感激!
I am trying to run a marco that check the cells users have entered and makes sure they are not blank. I am having a hard time with one cell. I want the user to be limited to entering only 2 letters and I want it to check to make sure no numbers were entered in that cell otherwise throw up an error message and exit sub. Any help is greatly appreciated!
If Worksheets("New PN").Range("B12").Value = "" Then
MsgBox "Date cannot be left empty.", vbOKOnly + vbExclamation, "Entry Error"
Exit Sub
End If
推荐答案
尝试这!
cellContent = Worksheets("New PN").Range("B12").Value
leftCC = Left(cellContent, 1)
rightCC = Right(cellContent, 1)
If Len(cellContent) <> 2 Then
MsgBox "There needs to be 2 characters."
Exit Sub
ElseIf (Asc(leftCC) < 65 Or Asc(leftCC) > 122 _
Or (Asc(leftCC) > 90 And Asc(leftCC) < 97)) _
Or _
(Asc(rightCC) < 65 Or Asc(rightCC) > 122 _
Or (Asc(rightCC) > 90 And Asc(rightCC) < 97)) Then
MsgBox "Both characters can only be letters."
Exit Sub
End If
可能又大又吓人,但这将使工作100%完成.
Might be big and scary, but it will get the job done 100%.
Asc(char)
公式返回所提供字符的ascii号. a-z和A-Z的外部限制为65和122,但是中间包含一些非字符(即[,\,],^,_,`).因此,可怕的是.
EDIT : The Asc(char)
formula returns the ascii number for the character supplied. The outer limits of a-z and A-Z are 65 and 122, however some non-characters are included in the middle (namely [, \, ], ^, _, `). Hence the horrid if.
这篇关于验证用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!