简体   繁体   中英

Check if cell is only a-z excel

I would like to be sure that all my cell contain only characters (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:

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.

EDIT : Also, you want to be certain you have not set Option Compare Text in your code. The default is Option Compare Binary which is what you want for this type of comparison. I have added that to the code for completeness.

  • Open the VBA editor (Alt+F11) and create a new module.
  • Add a reference to "Microsoft VBScript Regular Expressions 5.5" (Tools -> References).
  • 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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