简体   繁体   English

检查单元格是否仅是Excel

[英]Check if cell is only a-z excel

I would like to be sure that all my cell contain only characters (AZ/az). 我想确保我所有的单元格只包含字符(AZ / az)。 I want to be sure there isn't any symbol, number or anything else. 我想确保没有任何符号,数字或其他任何东西。 Any tips? 有小费吗?

For example I have this "Š". 例如,我有这个“Š”。

As a VBA function, the following should work: 作为VBA功能,以下应该起作用:

Option Compare Binary
Function LettersOnly(S As String) As Boolean
    LettersOnly = Not S Like "*[!A-Za-z]*" And S <> ""
End Function

In using the function, S can be either an actual string, or a reference to the cell of concern. 在使用函数时,S可以是实际字符串,也可以是对所关注单元格的引用。

EDIT : Also, you want to be certain you have not set Option Compare Text in your code. 编辑 :另外,您想确定自己没有在代码中设置“ Option Compare Text The default is Option Compare Binary which is what you want for this type of comparison. 默认值为“ Option Compare Binary ,这是您希望进行这种类型的比较。 I have added that to the code for completeness. 为了完整性,我已将其添加到代码中。

  • Open the VBA editor (Alt+F11) and create a new module. 打开VBA编辑器(Alt + F11)并创建一个新模块。
  • Add a reference to "Microsoft VBScript Regular Expressions 5.5" (Tools -> References). 添加对“ Microsoft VBScript正则表达式5.5”的引用(工具->引用)。
  • In your new module, create a new function like this: 在新模块中,创建一个新函数,如下所示:

     Function IsAToZOnly(inputStr As String) As Boolean Dim pattern As String: pattern = "^[A-Za-z]*$" Dim regEx As New RegExp regEx.pattern = pattern IsAToZOnly = regEx.Test(inputStr) End Function 
  • Use the new function in your worksheet: 在工作表中使用新功能:

     =IsAToZOnly(A1) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM