我已经接近找到解决方案,但并不完全。我想在单独的名称字段中分隔第一个,中间(如果存在)和姓氏。数据&目前的结果:
Data FName LName
Doe,John John Doe
Doe,John A John A Doe
Doe,John Art, Jr John Art, Jr Doe
守则:
First_Name: Mid([Client Name],InStr([Client Name],",")+1)
Last_Name: Left([Client Name],InStr([Client Name],",")-1)
正如你所看到的,我现在并不专注于中间名,但是想在中间名/首字母的MName中捕获它。我发现网上很多选项都接近这个,但没有一个能完成数据的任务:
Data FName MName LName
Doe,John John Doe
Doe,John A John A Doe
Doe,John Art, Jr John Art Doe Jr
Doe,John A, Jr John A Doe JR
由于
答案 0 :(得分:0)
用空格替换逗号,并用单个空格替换生成的双空格。
然后开始根据空格分割出字符串。
假设:
总是会有至少一个名字和一个名字,如果有可能只有一个名字,我的代码中的IF就会解决这个问题。
姓氏(即jr)只会有一个后缀
Sub FixNames()
Dim MyString As String, FirstName As String, MiddlePart As String, Surname As String, X As Long
For X = 2 To Range("A" & Rows.Count).End(xlUp).Row
FirstName = "": MiddlePart = "": Surname = ""
MyString = Replace(Replace(Range("A" & X).text, ",", " "), " ", " ")
FirstName = Split(Replace(MyString, ",", " "), " ")(1)
Surname = Split(Replace(MyString, ",", " "), " ")(0)
If Len(MyString) - Len(Replace(MyString, " ", "")) >= 2 Then MiddlePart = Split(Replace(MyString, ",", " "), " ")(2)
If Len(MyString) - Len(Replace(MyString, " ", "")) > 2 Then Surname = Surname & " " & Split(Replace(MyString, ",", " "), " ")(3)
Range("B" & X).Formula = FirstName
Range("C" & X).Formula = MiddlePart
Range("D" & X).Formula = Surname
Next
End Sub
答案 1 :(得分:-1)
显式选项
子名称()
Dim name As String
Dim first_name As String
Dim mid_name As String
Dim last_name As String
name = "AA,BB,CC"
first_name = Left(name, InStr(name, ",") - 1)
mid_name = Mid(name, InStr(name, ",") + 1, InStrRev(name, ",") - InStr(name, ",") - 1)
last_name = Right(name, Len(name) - InStrRev(name, ","))
MsgBox first_name & Chr(10) & mid_name & Chr(10) & last_name
结束子