vba 如何检查字符串是否仅包含字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29633517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if a string only contains letters?
提问by Isaac Rothstein
I'm using a function that allows me to review a string of text and evaluate if it is composed of letters. It is housed in a module called "General". The general module only exists to house public functions and variables. Function code is listed below:
我正在使用一个函数,它允许我查看一串文本并评估它是否由字母组成。它位于一个名为“General”的模块中。通用模块仅用于容纳公共函数和变量。功能代码如下:
Public Function IsAlpha(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsLetter = True
Case Else
IsLetter = False
Exit For
End Select
Next
End Function
Next I have two "if" routines that evaluate the first 2 characters of a textbox in my userform. The first routine asks if the 1st character is numeric and the second routine asks if the 2nd character is alpha. Currently, the second "if" routine is ejecting me from the sub-routine when IsAlpha tests True, rather than generating the MsgBox. Is the IsAlpha function not being called correctly?
接下来,我有两个“if”例程,用于评估用户表单中文本框的前 2 个字符。第一个例程询问第一个字符是否为数字,第二个例程询问第二个字符是否为字母。当前,当 IsAlpha 测试为 True 时,第二个“if”例程将我从子例程中弹出,而不是生成 MsgBox。是否未正确调用 IsAlpha 函数?
If routines code listed below:
如果例程代码如下:
Private Sub CmdMap_Click()
With TxtDxCode
If IsNumeric(Left(Me.TxtDxCode.Text, 1)) Then
MsgBox "Incorrect DX Code format was entered. ", vbExclamation, "DX Code Entry"
TxtDxCode.Value = ""
TxtDxCode.SetFocus
Exit Sub
End If
If IsAlpha(Left(Me.TxtDxCode.Text, 2)) Then
MsgBox "Incorrect DX Code format was entered. ", vbExclamation, "DX Code Entry"
TxtDxCode.Value = ""
TxtDxCode.SetFocus
Exit Sub
End If
End With
回答by Sam
Why don't you use regular expressions instead? Then there's no loops involved:
为什么不使用正则表达式呢?然后不涉及循环:
Public Function IsAlpha(strValue As String) As Boolean
IsAlpha = strValue Like WorksheetFunction.Rept("[a-zA-Z]", Len(strValue))
End Function
Also, when you create a User-Defined Function (UDF) you need to ensure that the return value is assigned to the name of the actual function, in this case IsAlpha- not IsLetterotherwise the value will never be passed back.
此外,当您创建用户定义函数 (UDF) 时,您需要确保将返回值分配给实际函数的名称,在这种情况下IsAlpha-IsLetter否则该值将永远不会被传回。
回答by Saagar Elias Hymany
Try this for IsAlpha
试试这个 IsAlpha
Public Function IsAlpha(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsAlpha = True
Case Else
IsAlpha = False
Exit For
End Select
Next
End Function
回答by Excel Hero
The most direct, concise, and performant way to check if a string contains only alpha characters is with this function:
检查字符串是否仅包含字母字符的最直接、简洁和高效的方法是使用此函数:
Function IsAlpha(s) As Boolean
IsAlpha = Len(s) And Not s Like "*[!a-zA-Z]*"
End Function
回答by barrowc
Left(Me.TxtDxCode.Text, 2)returns the first two characters of the string. So, if Me.TxtDxCode.Text was 7ZABC, this expression would return "7Z". This would cause the IsAlphatest to fail.
Left(Me.TxtDxCode.Text, 2)返回字符串的前两个字符。因此,如果 Me.TxtDxCode.Text 是 7ZABC,则此表达式将返回“7Z”。这会导致IsAlpha测试失败。
As you want to examine just the 2nd character, use Mid$ instead:
由于您只想检查第二个字符,请改用 Mid$:
If IsAlpha(Mid$(Me.TxtDxCode.Text, 2, 1)) Then
If IsAlpha(Mid$(Me.TxtDxCode.Text, 2, 1)) Then
This will return "Z" and the IsAlphatest should now succeed
这将返回“Z”并且IsAlpha测试现在应该成功
(The string versions Left$, Mid$ etc are slightly faster than the variant versions Left, Mid etc - see here)
(字符串版本 Left$、Mid$ 等比变体版本 Left、Mid 等稍快 - 请参阅此处)

