从Excel中提取名称

我必须在名称和电子邮件地址组合的列中提取客户名称。 此列中显示内容的示例可能如下所示:

John Smith Johnsmith@me.com

Joe Bloggs theycallmejoe@myemail.com

Justin Credible JustinC@provider.com

我发现这个很酷的VBA提取电子邮件地址。

 Function ExtractEmailAddress(s As String) As String Dim AtSignLocation As Long Dim i As Long Dim TempStr As String Const CharList As String = "[A-Za-z0-9._-]" 'Get location of the @ AtSignLocation = InStr(s, "@") If AtSignLocation = 0 Then ExtractEmailAddress = "" 'not found Else TempStr = "" 'Get 1st half of email address For i = AtSignLocation - 1 To 1 Step -1 If Mid(s, i, 1) Like CharList Then TempStr = Mid(s, i, 1) & TempStr Else Exit For End If Next i If TempStr = "" Then Exit Function 'get 2nd half TempStr = TempStr & "@" For i = AtSignLocation + 1 To Len(s) If Mid(s, i, 1) Like CharList Then TempStr = TempStr & Mid(s, i, 1) Else Exit For End If Next i End If 'Remove trailing period if it exists If Right(TempStr, 1) = "." Then TempStr = _ Left(TempStr, Len(TempStr) - 1) ExtractEmailAddress = TempStr End Function 

但是我需要类似的东西来提取名称。

任何人都可以协助

只需通过删除电子邮件地址,另一个小函数获取名称。 见下文…

在工作表中使用函数。 在这里输入图像说明

 Function GetName(refCell As String) Dim tempName As String tempName = Trim(Left(refCell, Len(refCell) - Len(ExtractEmailAddress(refCell)))) GetName = tempName End Function '---------------------------------------------------------- Function ExtractEmailAddress(s As String) As String Dim AtSignLocation As Long Dim i As Long Dim TempStr As String Const CharList As String = "[A-Za-z0-9._-]" 'Get location of the @ AtSignLocation = InStr(s, "@") If AtSignLocation = 0 Then ExtractEmailAddress = "" 'not found Else TempStr = "" 'Get 1st half of email address For i = AtSignLocation - 1 To 1 Step -1 If Mid(s, i, 1) Like CharList Then TempStr = Mid(s, i, 1) & TempStr Else Exit For End If Next i If TempStr = "" Then Exit Function 'get 2nd half TempStr = TempStr & "@" For i = AtSignLocation + 1 To Len(s) If Mid(s, i, 1) Like CharList Then TempStr = TempStr & Mid(s, i, 1) Else Exit For End If Next i End If 'Remove trailing period if it exists If Right(TempStr, 1) = "." Then TempStr = _ Left(TempStr, Len(TempStr) - 1) ExtractEmailAddress = TempStr End Function 

你也可以使用内置函数来代替GetNameExtractEmailAddress一样

 =TRIM(LEFT(A1,LEN(A1)-LEN(ExtractEmailAddress(A1)))) 

在这里输入图像说明

公式只有方法依赖于这个超级用户的答案有关查找单元格中的最后一个空间。 然后,您只需使用LEFT(position_of_last_space-1)来获取电子邮件地址左侧的所有内容。

 =LEFT(A1,FIND("`",SUBSTITUTE(A1," ","`",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1) 

使用back-tick可以替代空格。 假设是名称或电子邮件地址中没有备份。

例:

在这里输入图像说明

你有没有尝试这种非Flash的FBA解决scheme?

如果您的数据具有相同的模式(例如,您想要提取名字的电子邮件地址),则:

  • 在下一列写你想要提取的例子(例如John Smith):
  • select该单元格,然后按Ctrl + E
  • 你会得到清单,你可以进一步检查。

在这里输入图像说明