[英]How can I check if a cell in Excel spreadsheet contains number
I have a column of addresses and I have to find those which don't contain street numbers. 我有一列地址,我必须找到那些不包含街道号码的地址。 Unfortunately, addresses have been input by various users and they do not follow the same pattern so the street type, street name, suburb are in different order and I can't use functions like LEFT, RIGHT or MID to check if particular character is a number.
不幸的是,地址已被各种用户输入,并且它们不遵循相同的模式,因此街道类型,街道名称,郊区的顺序不同,我不能使用LEFT,RIGHT或MID等功能来检查特定字符是否是数。 The column looks like this:
该列如下所示:
10 Willsons Drive, Manhattan
Epping, 23 Wet Rd
Longsdale St, Kingsbury
11 Link Crt, Pakenham
Is there an Excel or VBA function that can tell me if cell / string contains numbers? 是否有Excel或VBA函数可以告诉我单元格/字符串是否包含数字?
Put this into a Module, then in your worksheet, may be a column next to it, put formula =HaveNumbers(A2)
and check if you want it like that (True/False). 把它放到一个模块中,然后在你的工作表中,可能是它旁边的一列,放入formula
=HaveNumbers(A2)
并检查你是否想要它(True / False)。 You can change it to Return a String instead. 您可以将其更改为返回字符串。 This Returns TRUE / FALSE.
返回TRUE / FALSE。
Function HaveNumbers(oRng As Range) As Boolean
Dim bHaveNumbers As Boolean, i As Long
bHaveNumbers = False
For i = 1 To Len(oRng.Text)
If IsNumeric(Mid(oRng.Text, i, 1)) Then
bHaveNumbers = True
Exit For
End If
Next
HaveNumbers = bHaveNumbers
End Function
There isn't a single VBA function that will do what you want, but the following function should do the trick: 没有一个VBA函数可以执行您想要的操作,但以下函数应该可以解决问题:
Public Function ContainsNumbers(Inp As String) As Boolean
Dim Strings() As String, Str As Variant
Strings = Split(Inp, " ")
For Each Str In Strings
If IsNumeric(Str) Then
ContainsNumbers = True
Exit For
End If
Next
End Function
Then put something like =ContainsNumbers(A1)
in a nearby cell. 然后在附近的单元格中添加类似
=ContainsNumbers(A1)
的内容。
Thanks Monty. 谢谢蒙蒂。 In my case, though, numbers were not always separated from words so I had to iterate over each character.
在我的情况下,数字并不总是与单词分开,所以我不得不迭代每个字符。 I used following:
我使用以下:
Function ContainsNumber(text As String)
'checks if given cell contains number
For i = 1 To Len(text)
If IsNumeric(Mid$(text, i, 1)) Then
ContainsNumber = True
Exit Function
End If
Next
ContainsNumber = False
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.