简体   繁体   中英

Check if cell contains other characters than a-z; 0-9

I am searching for a way in excel to check a cell for other characters than the alphabet az, numbers 0-9 and the character "-"

In column "A" I have a list of product names like

A1: samsung-s7-black

A2: apple-phone-6-silver

A3: huawei-p9-limited-edition!

In column "B" I would like to get the following info

B1:

B2:

B3: !

Basically I am looking for a "negative" search in which i don't define which characters are not allowed but more which characters are allowed in my cell and output the characters that do not match. If this could be done without VBA even better.

If you have Office 365 / Excel 2016, you can use the TEXTJOIN function in an array formula:

B1: =TEXTJOIN("",TRUE,IF((CODE(MID(A3,seq,1))>=97)*(CODE(MID(A3,seq,1))<=122)+(CODE(MID(A3,seq,1))=45)+ISNUMBER(--MID(A3,seq,1))=1,"",MID(A3,seq,1)))

Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter . If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar

seq is a Name'd formula that refers to:

   =ROW(INDEX(Sheet1!$1:$65535,1,1):INDEX(Sheet1!$1:$65535,LEN(INDIRECT("RC[-1]",FALSE)),1))

Note that we use the RC version of INDIRECT so the formula needs to be placed in the adjacent column of the string being tested.

在此处输入图片说明

Oh, and if you have mixed case in your actual data, replace A1 in the formula with =LOWER(A1)

=TEXTJOIN("",TRUE,IF((CODE(MID(LOWER(A1),seq,1))>=97)*(CODE(MID(LOWER(A1),seq,1))<=122)+(CODE(MID(LOWER(A1),seq,1))=45)+ISNUMBER(--MID(LOWER(A1),seq,1))=1,"",MID(LOWER(A1),seq,1)))

If you do not have the TEXTJOIN function, you could do a nested SUBSTITUTE or use a VBA solution.

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a",""),"b",""),"c",""),"d",""),"e",""),"f",""),"g",""),"h",""),"i",""),"j",""),"k",""),"l",""),"m",""),"n",""),"o",""),"p",""),"q",""),"r",""),"s",""),"t",""),"u",""),"v",""),"w",""),"x",""),"y",""),"z",""),"0",""),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),"-","")

Here's a VBA solution. Put this in a workbook module, and you can call with =remove_alphanumeric(A1)

Function remove_alphanumeric(InputString As String) As String
Dim i As Integer, strLen As Integer
Dim tmp_str As String, final As String
final = ""
i = 1
strLen = Len(InputString)
For i = 1 To strLen
    tmp_str = Mid(InputString, i, 1)
    If InStr(1, "abcdefghijklmnopqrstuvwxyz0123456789-", tmp_str) = 0 Then final = final + tmp_str
Next
remove_alphanumeric = final
End Function

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