Check if a text string contains special characters

2019-09-16 06:37发布

As the title already mentioned, I want to check if a certain text string contains any special characters.

Currently I have created a column with the characters which are allowed (ASCII range 32-122). And I would like a formula which returns 'ok', if the cell only contains characters which are allowed and 'not ok' if the cell contains any character that is not allowed.

However, Is this even possible without using a for loop? (thus without vba)

3条回答
Evening l夕情丶
2楼-- · 2019-09-16 06:54

With your string to check in A1:

=IF(SUMPRODUCT((CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))>=32)*(CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<=132))=LEN(A1),"OK","Not OK")

EDIT If the character strings to be checked might include Unicode characters, then you must use the UNICODE function in place of the CODE function. I believe this function became available in Excel 2013.

=IF(SUMPRODUCT((UNICODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))>=32)*(UNICODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<=132))=LEN(A1),"OK","Not OK")
查看更多
Ridiculous、
3楼-- · 2019-09-16 07:03

Assuming that your list of valid characters is in A1:A91 and that the first entry for which you wish to perform the test is in B1, enter this formula in C1:

=IF(LEN(B1)=0,"",REPT("not ",SUMPRODUCT(0+(ISNUMBER(MATCH(MID(B1,ROW(INDEX(B:B,1):INDEX(B:B,LEN(B1))),1),$A$1:$A$91,0))))<>LEN(B1))&"ok")

Copy down to give similar results for strings in B2, B3, etc.

We can in fact do without the use of an explicit list within the worksheet, viz:

=IF(LEN(B1)=0,"",REPT("not ",SUMPRODUCT(0+(ABS(77-CODE(MID(B1,ROW(INDEX(B:B,1):INDEX(B:B,LEN(B1))),1)))>45)))&"ok")

Regards

查看更多
萌系小妹纸
4楼-- · 2019-09-16 07:05

Use OR with FIND function and return with IF function

查看更多
登录 后发表回答