从文本中提取名称

时间:2014-09-05 06:50:17

标签: excel vba excel-vba substring extract

这是一个单元格中包含的示例数据:

  

2014/08/19 12:59 John Doe
  添加sample@hotmail.com

我需要在文本中提取名称。我知道它总是放在日期时间戳之后。

我的想法是找到"的位置:"并添加4,从而得到名字的第一个字母的位置:

colonLoc = InStr(sampleData, ":")
firstLetterLoc = colonLoc + 4

如何在此之后获得名字和姓氏?

5 个答案:

答案 0 :(得分:6)

这是一个实现你想要的内容。

debug.print Mid(Split(Split(Range("A1").Value, Chr(10))(0), ":")(1), 3)

enter image description here

修改

实际上你不需要VBA。您也可以使用Excel公式

=MID(A1,FIND(":",A1)+3,FIND(CHAR(10),A1)-(FIND(":",A1)+3))

enter image description here

答案 1 :(得分:4)

即使对于带空格的名称也是如此:

Function ExtractName(str As String) As String
    Dim i As Long
    Dim splitStr() As String
    Dim nameParts() As String
    splitStr = Split(str, " ")
    ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 4)
    For i = LBound(nameParts) To UBound(nameParts)
        nameParts(i) = splitStr(i + 2)
    Next i
    ExtractName = Join(nameParts, " ")
End Function

这实际上是删除四个子字符串:日期,时间,add位和电子邮件地址。假设中间的其他所有内容都是名称的一部分。

使用示例:

Debug.Print ExtractName("2014/08/19 12:59 John Doe add sample@hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Johan Sebastian Bach add sample@hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Fuh Wei Guo Tang add sample@hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 Jens von dem Hagen add sample@hotmail.com")
Debug.Print ExtractName("2014/08/19 12:59 José Manuel de Santiago Itthuralde add sample@hotmail.com")

编辑现在你说你的输入字符串被分成两行...这对我来说对你指定了输入:

Function ExtractName(str As String) As String
    Dim i As Long
    Dim splitStr() As String
    Dim nameParts() As String
    splitStr = Split(Split(str, vbLf)(0), " ")
    ReDim nameParts(LBound(splitStr) To UBound(splitStr) - 2)
    For i = LBound(nameParts) To UBound(nameParts)
        nameParts(i) = splitStr(i + 2)
    Next i
    ExtractName = Join(nameParts, " ")
End Function

答案 2 :(得分:0)

sampleData = "2014/08/19 12:59 John Doe add sample@hotmail.com"

New_String = Split(sampleData)

sName = New_String(2) & " " & New_String(3)

Debug.Print sName

这样简单:)

答案 3 :(得分:0)

就是这样:

Option Explicit

Public Sub Playground()
    Const SampleData As String = "2014/08/19 12:59 John Doe add sample@hotmail.com"
    Dim Parts() As String
    Dim FirstName As String
    Dim LastName As String

    Parts = Split(SampleData)
    FirstName = Parts(2)
    LastName = Parts(3)

    Debug.Print FirstName
    Debug.Print LastName
End Sub

对于更复杂的情况(例如名称中的空格),您可能需要稍微调整一下。

答案 4 :(得分:0)

这将为您提供名字(假设只有1),姓氏(所有其他名称)和变体数组中的电子邮件

Option Explicit

Public Function Name(source As String) As Variant
    Dim breakup As Variant

    breakup = Split(source, ":")
    breakup = Split(Mid(breakup(1), 4), " ")

    Dim i As Integer
    Dim FirstName As String
    FirstName = breakup(0)

    Dim LastName As String
    For i = 1 To UBound(breakup) - 2
        LastName = LastName & " " & breakup(i)
    Next
    LastName = Mid(LastName, 2)

    Dim Email As String
    Email = breakup(UBound(breakup))

    Name = Array(FirstName, LastName, Email)
End Function