我希望用户只在excel单元格中输入字符.. 我在excel的自定义公式中使用 ISTEXT(值)但是无法获得正确的字符串输出,例如 hgdashj2132154 请帮助... 谢谢你..
答案 0 :(得分:3)
它需要一点点正规表达魔法。您需要用户定义的函数和正则表达式加载项。在Developer视图中,转到Tools | References并勾选Microsoft VBScript Regular Expressions 5.5。
这是功能:
Public Function isOnlyAlpha(myText) As Boolean
Dim regEx
Set regEx = New RegExp 'Regular expression object
regEx.Pattern = "^[a-zA-Z]*$" ' Set pattern.
isOnlyAlpha = regEx.Test(myText) ' Test for match
End Function
结果:
=isOnlyAlpha("hgdashj2132154")
False
=isOnlyAlpha("hgdashj")
True
如果您不想使用正则表达式,那么总会出现“Like Loop”:
Public Function IsOnlyAlpha(Value As String) As Boolean
IsOnlyAlpha = True
Dim i As Integer
For i = 1 To Len(Value)
IsOnlyAlpha = IsOnlyAlpha And (Mid(Value, i, 1) Like "[A-Za-z]")
Next i
End Function
答案 1 :(得分:2)
Smack,我在我的书签网站上找到了这个。它与Marc Thibault的建议基本相同,但不需要任何其他参考:
答案 2 :(得分:1)
纯VBA方法:
(我编辑了我的答案,使用Like运算符而不是IsNumeric函数。现在只有字符a到z被认为是有效的,而不仅仅是非数字字符。)
Public Function IsOnlyAlpha(Value As String) As Boolean
If Len(Value) = 0 Then
IsOnlyAlpha = False
Exit Function
End If
Dim i As Integer
For i = 1 To Len(Value)
If Not Mid(Value, i, 1) Like "[A-Za-z]" Then
IsOnlyAlpha = False
Exit Function
End If
Next i
IsOnlyAlpha = True
End Function