2017-03-21 108 views
3
  • 我有列中有评论(超过5000例)。
  • 那些评论有文字,数字,日期,一切。
  • 我需要从这些评论中获得电话号码。
  • 电话号码是随机的地方为每一个评论,所以LEFTMIDRIGHT将无法​​正常工作

,我已经达到最接近的结果是Kutools =EXTRAXTNUMBERS() ......但我得到一个线包括日期,ID等的数字。从评论中提取电话号码

宁愿一个公式。 :)下面

两个样品的意见,需要的电话号码是大胆

周四,2017年2月2日下午2:37协调世界时0.3868 ,个人.pārv.Tatjana通话选举结果:Norunacitālaikā - 2017-02-03 07:15 2017年2月8日星期三上午08:18协调世界时.3868 nr。 -neeksistē,personāladaļasvad.Tatjana neatbild,arīnr。 调用结果选择:2017年2月10日星期五上午7:15协调世界时* .3868 *** piezv ap 13调用结果选择:Norunacitālaikā - 2017-02-10 11:15

2017年2月2日星期四11:15协调世界时4213zvanīt调用结果选择:Norunacitālaikā - 2017-02-07 09:00 2017年2月14日星期二下午12:59协调世界时.4532 *梅艳芳@ dzintarniece @ rtp.lv呼叫结果选择:Turpinātinternetā

+1

正版Excel的评论或只是细胞与它们混合文本? –

+0

其中包含混合文本的单元格。 –

+0

数字始终是注释中第一个8位数字的序列?也许你可以用它作为提取规则..它看起来像他们总是在单词“时间”之后,并在“呼叫”一词之前。 –

回答

1

使用的UDF可以使用下面的代码实现这一点:

要使用它:

  1. ALT + F11
  2. 插入模块
  3. 粘贴代码
  4. 在Excel工作表,使用这个公式=get_phone("CELL_WITH_NUMBER_HERE")获得的8个位数的第一个序列中的细胞。

代码:

Public Function get_phone(cell As Range) 
    Dim s As String 
    Dim i As Integer 
    Dim num 
    Dim counter As Integer 

    'get cell value 
    s = cell.Value 

    'set the counter 
    counter = 0 
    'loop through the entire string 
    For i = 1 To Len(s) 
     'check to see if the character is a numeric one 
     If IsNumeric(Mid(s, i, 1)) = True Then 
      'add it to the number 
      num = num + Mid(s, i, 1) 
      counter = counter + 1 
      'check if we've reached 8 digits 
      If counter = 8 Then 
       get_phone = num 
       Exit Function 
      End If 
     Else 
     'was not numeric so reset counter and answer 
     counter = 0 
     num = "" 
     End If 
    Next i 
End Function 

示例图像:

enter image description here

4

这个小UDF()将返回所有的8位数字的子串字符串:

Public Function PHNum(s As String) As String 
    Dim L As Long, i As Long, temp As String 
    Dim CH As String 
    L = Len(s) 
    temp = "" 
    PHNum = "" 
    For i = 1 To L 
     CH = Mid(s, i, 1) 
     If IsNumeric(CH) Then 
      temp = temp & CH 
      If Len(temp) = 8 Then 
       PHNum = PHNum & vbCrLf & temp 
      End If 
     Else 
      temp = "" 
     End If 
    Next i 
End Function 

enter image description here

注:

为了获得在输出单元堆叠格式,格式,它包上。

+0

这就是我需要的。谢谢! –

+0

@EmīlsBisenieks你很受欢迎。 –

4

正则表达式解决方案

这UDF提取到您的电话号码从文本,作为一个数组。您最终可以使用Join将其转换为csv字符串,也可以将该数组粘贴到一系列单元格中。

Function extractPhones(s As String) As String() 
    Dim i As Long, matches, match, ret 
    With CreateObject("VBScript.Regexp") 
     .Global = True 
     .Pattern = "\W[26]\d{7}\W" 
     Set matches = .Execute(s) 
    End With 
    ReDim ret(1 To matches.Count) As String 
    For Each match In matches 
     i = i + 1 
     ret(i) = Mid(match, 2, Len(match) - 2) 
    Next 
    extractPhones = ret 
End Function 

它使用匹配与这些规格电话号码的正则表达式:

  • 是完全相同8位数字
  • 开始由6或2
  • 不是由一个字母数字字母之前或之后,但由空白或标点符号。

demo

-1

有附加在Excel中,我在过去的正则表达式(http://seotoolsforexcel.com/regexpfind/)使用。在你的情况下,它可能会很复杂,因为你不知道电话号码在你的手机中会出现多少次。对于这些情况,我建议你使用其他用户提供的VBA脚本。

1

另一个选项,返回所有匹配的单细胞

enter image description here

https://regex101.com/r/Hdv65h/1

Function StrPhone(strIn As String) As String 
    Dim objRegexp As Object 
    Set objRegexp = CreateObject("VBScript.Regexp") 
    With objRegexp 
     .Global = True 
     .Pattern = ".*?(\d{8})|.*$" 
     StrPhone = Trim(.Replace(strIn, "$1 ")) 
    End With 
End Function