ActiveCell.Value = InStr(1, ActiveCell.Offset(0, -1).Value, " Mrs ", 1)
LengthOfString = Len(ActiveCell.Offset(0, -1).Value)
If (ActiveCell.Value > 1 And complete = 0) Then
ActiveCell.Offset(0, 1).Value = "Mr " & Mid(ActiveCell.Offset(0, -1).Value, 4, LengthOfString - (LengthOfString - ActiveCell.Value) - 4)
ActiveCell.Offset(0, 2).Value = "Mrs " & Right(ActiveCell.Offset(0, -1).Value, (LengthOfString - ActiveCell.Value - Len(" Mrs ") + 1))
complete = 1
'ActiveCell.Offset(0, 3).Value = LengthOfString - ActiveCell.Value - 1
End If
上面是我设法编写的变体之一,以拆分如下所示的变体;
Mr and Mrs Smith Mr & Mrs Smith Mr John Smith & Mrs Smith Mr John Smith and Mrs Smith
...等等。
该公式基本上采用“先生”,任何名字和姓氏,并将它们串联在下一个单元格中。
它一直运行良好,但是我现在面临的问题是
的变体Mr and Mrs Arlene Smith
我尝试了几种不同的方法,但似乎无法正常工作。而不是花力气读“史密斯先生”,而是得到“阿琳史密斯先生”的结果。
但是,确实可以正确拉过“阿琳·史密斯夫人”。
答案 0 :(得分:0)
尝试此代码:
Dim test As String
test = "Mr and Mrs Arlene Smith"
If InStr(test, "and") > 1 Then
ActiveCell.Offset(0, 1).Value = Trim(Split(test, "and")(0)) 'Mr part
ActiveCell.Offset(0, 2).Value = Trim(Split(test, "and")(1)) 'Mrs part
ElseIf InStr(test, "&") > 1 Then
ActiveCell.Offset(0, 1).Value = Trim(Split(test, "&")(0))
ActiveCell.Offset(0, 2).Value = Trim(Split(test, "&")(1))
End If
If ActiveCell.Offset(0, 1).Value = "Mr" Then
'If "Mr part" is only "Mr", take last word from "Mrs part" and concatenate it with "Mr"
ActiveCell.Offset(0, 1).Value = "Mr " & Split(ActiveCell.Offset(0, 2).Value, " ")(UBound(Split(ActiveCell.Offset(0, 2).Value, " ")))
End If
请注意,仅当字符串中包含“ and”或“&”并且姓氏始终是最后一个单词时,该功能才起作用。