数字在单元格中的有效性

我有一个excel的手机号码列的成员表,我需要输出的手机号码是无效的(less于10位数以上)的成员。

任何人都可以提出这样做​​的最佳方式?

有一个公式来计算一个单元格的数字位数?

试试这个为您的RegEx:

/(?:(?<= [+][1-9])|(?<= [1-9])|(?<= ))([2-9][0-9]{9})\b/gi 
 Public Function IsValidPhone(xlCell as Range) as Boolean If (xlCell.range Like "#[-.]###[-.]###[-.]###") OR (xlCell.range2 Like "#[-.]###[-.]###[-.]###") Then IsValidPhone = True ELSE IsValidPhone = False End Function 
 =AND(ISNUMBER(A1),LEN(A1)=10) 

这将testing单元格a1是否是数字,长度是10位数。 你可以把它拖下来,并过滤所有的错误。 如果你有其他有效的字符如“ – ”,那么把它们添加到你的OP中,我们也会处理它。

在项目或全局Module创build一个VBAmacros/函数 。 如果你没有一个,那么只需创build/添加一个(默认情况下它将被称为Module1–没关系)。

然后创build下面的function。 然后传递一个像"(?<!\S)(?:\+?1)?([2-9]\d{9})(?!\S)"

 (?<! \S ) // Look behind, whitespace or ^ (?: \+? 1 )? // Optional +1 ( [2-9] \d{9} ) // (1), ten digit phone (?! \S ) // Look ahead, whitespace or $ 

 '// Executes a Regular Expression on a provided string '// and returns a selected submatch: '// `str` - string to execute the regex on '// `reg` - the regular expression with at least 1 capture '()' '// `matchIndex` - the index of the match you want to return (default: 0) '// `subMatchIndex` - the index of the submatch you want to return (default: 0) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RegexExecute( _ str As String, reg As String, Optional matchIndex As Long, _ Optional subMatchIndex As Long _ ) As String On Error GoTo ErrHandl Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = reg regex.Global = Not (matchIndex = 0 And subMatchIndex = 0) '//For efficiency If regex.test(str) Then Set matches = regex.Execute(str) RegexExecute = matches(matchIndex).SubMatches(subMatchIndex) Exit Function End If ErrHandl: RegexExecute = CVErr(xlErrValue) End Function 

非VBA公式解决scheme忽略电话号码格式,并将获得一个单元格中的位数:

 =SUMPRODUCT(LEN(A1)-LEN(SUBSTITUTE(A1,{1,2,3,4,5,6,7,8,9,0},""))) 

例如,它可以在以下所有方面成功运行:

 1234567890 123-456-7890 (123)4567890 (123) 456-7890 123.456.7890 123 456 7890