简体   繁体   English

检查字符串是否包含数字

[英]Check if string contains number

I have a big address file with street + housenumber in one column. 我在一列中有一个带有street + housenumber的大地址文件。 I want to check how many addresses there are without housenumber . 我想检查没有housenumber的地址

So actually I'm just trying to check if the cells contain a number anywhere 所以实际上我只是想检查细胞是否包含任何数字

I tried a number of things, 我尝试了很多东西,

=COUNTIF(A1:A500;{0,1,2,3,4,5,6,7,8,9})
=COUNTIF(A1:A500;"*>0*")

but none seemed to work 但似乎都没有效果

Streetname 5 should return TRUE
5 Streetname should return TRUE
Streetname five should return FALSE
Streetname should return FALSE

Try this 尝试这个

=COUNT(FIND({0,1,2,3,4,5,6,7,8,9},A1))>0

You need to check each cell individually. 您需要单独检查每个单元格。 Using the curly brackets generates an array so if any digit is found, FIND returns a number and COUNT goes up by 1. 使用花括号生成一个数组,因此如果找到任何数字,FIND返回一个数字,COUNT增加1。

If no digits are found, COUNT returns 0 and the formula shows FALSE. 如果未找到数字,则COUNT返回0,公式显示为FALSE。

在此输入图像描述

To my mind the cleanest way to do this is to use regular expressions. 在我看来,最干净的方法是使用正则表达式。 Add a VBA module to the workbook with the following function: 使用以下函数将VBA模块添加到工作簿:

Public Function rxTest(inputString As String, pattern As String) As Boolean
    Dim r As New RegExp

    r.pattern = pattern
    rxTest = r.Test(inputString)
End Function

(you will need to add a reference to Microsoft VBScript Regular Expressions ). (您需要添加对Microsoft VBScript Regular Expressions的引用)。 Then the following formula will do what you want for each cell and you can count the number of TRUE cells: 然后,下面的公式将为每个单元格执行您想要的操作,您可以计算TRUE单元格的数量:

= rxTest(A1,"\d")

You were very close with your COUNTIF function. 你的COUNTIF功能非常接近。 But since your function contains an array constant, it will return an array of values. 但由于您的函数包含数组常量,因此它将返回一个值数组。

To check how many addresses there are without housenumber : 查看没有housenumber的地址数

=COUNTA(A1:A100)-SUM(N(COUNTIF(A1:A100,"*" &{0,1,2,3,4,5,6,7,8,9}&"*")>0))

To return TRUE/FALSE depending on the contents of an individual cell, you can use this formula: 要根据单个单元格的内容返回TRUE/FALSE ,可以使用以下公式:

=OR(COUNTIF(A1,"*" &{0,1,2,3,4,5,6,7,8,9}&"*")>0)

Note as pointed out in a comment to another question, this will only detect if there is a digit in the string. 请注意 ,在对另一个问题的评论中指出,这只会检测字符串中是否有数字。 It will not differentiate strings where the street name includes a number (eg 5th Ave ) from strings that include address numbers. 它不会区分街道名称包含数字(例如, 第五大道 )的字符串和包含地址编号的字符串。 It also won't detect an address where the address number is spelled out (eg One Drury Avenue ) 它也不会检测地址编号拼写的地址(例如One Drury Avenue

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

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