简体   繁体   English

检查单元格是否包含除z以外的其他字符; 0-9

[英]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 "-" 我在excel中寻找一种方法来检查单元格中是否存在除字母az,数字0-9和字符“-”以外的其他字符

In column "A" I have a list of product names like 在“ A”列中,我列出了产品名称,例如

A1: samsung-s7-black A1:三星S7黑色

A2: apple-phone-6-silver A2:苹果手机6银

A3: huawei-p9-limited-edition! A3:huawei-p9限量版!

In column "B" I would like to get the following info 我想在“ B”列中获得以下信息

B1: B1:

B2: B2:

B3: ! 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. 如果没有VBA甚至可以做得更好。

If you have Office 365 / Excel 2016, you can use the TEXTJOIN function in an array formula: 如果您拥有Office 365 / Excel 2016,则可以在数组公式中使用TEXTJOIN函数:

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 . 由于这是一个数组公式,因此需要在按下Enter键的同时按住ctrl + shift来“确认”它。 If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar 如果正确执行此操作,Excel将按照公式栏中的说明在公式周围放置括号{...}

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. 请注意,我们使用RC版本的INDIRECT因此该公式需要放置在要测试的字符串的相邻列中。

在此处输入图片说明

Oh, and if you have mixed case in your actual data, replace A1 in the formula with =LOWER(A1) 哦,如果您在实际数据中混合使用大小写,请将公式中的A1替换为=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. 如果没有TEXTJOIN函数,则可以执行嵌套的SUBSTITUTE或使用VBA解决方案。

=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. 这是VBA解决方案。 Put this in a workbook module, and you can call with =remove_alphanumeric(A1) 将其放在工作簿模块中,可以使用=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

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

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