我的任务是进行一些字符串操作,今天一定是我糟糕的一天,因为事实证明它比我想象的要困难。
我必须从第一,第二和第三列以及任何姓氏中取出第一个,第二个和第三个名字的首字母
另外我们需要保留标题。
以下是现在的长名称示例:
C Chrysostomou先生& N Chrysostomou先生& A Chrysostomou女士
M Karseras夫人& P Hadjisoteriou女士& E Athanasiou女士
Theodorou夫人& A Aristotelou先生& Gazaz女士& M Karmiou L Vazanias夫人& G太太
Braithwaite& Helen West夫人L Vazanias夫人& G Braithwaite女士&杜
Helen West Mrs Olympia Pieridou& T& Mr M& Mr C&先生K夫人
Michaelides Miss J A Santamas& M T Santama-Solomonides女士&丽达太太
Santama Miss J A Santamas& M T Santama-Solomonides女士&丽达太太
Santama Mr Polydoros Polydorou& Maro Themistocleous夫人&西尔维亚夫人
Polydorou Themis先生&安德鲁拉夫人&尼古拉斯先生和Vasso Gina女士
Demetriou S K Makkofaides夫人& Z Koullas先生& Y Koullas夫人& R太太
Kleopa Mr G Zorzy& H Louca Zorzy夫人& S Stavropoulos先生& Y太太
Stavropoulos Mrs M Franceschina& C Eugeniou女士& O L Toumazides女士
T / a The Three Cupcakes Mr. David& Eileen Nixon D.h.nixon夫人&公司 - 办公室帐户
如您所见,这些可被视为2人甚至3人之间的联合银行账户。我们将不得不保留这一点,这可能是先生,小姐,女士,博士,博士或 Messrs 以及第一和第二名称的首字母和全部姓氏,总数应少于35个字符!
所以,这是我在网上搜索后一直在尝试的内容:
=IF(LEN(TRIM(E:E))-LEN(SUBSTITUTE(TRIM(E:E)," ",""))>=1,MID(TRIM(E:E),FIND(" ",TRIM(E:E))+1,1),"")& " " &IF(LEN(TRIM(E:E))-LEN(SUBSTITUTE(TRIM(E:E)," ",""))>=2,MID(SUBSTITUTE(TRIM(E:E)," ","",1),FIND(" ",SUBSTITUTE(TRIM(E:E)," ","",1))+1,1),"")
获得缩写,但只有前2个
=RIGHT(J:J,LEN(J:J)-FIND(" ",J:J)+1)
获取姓氏但工作不正常。
我是在想这个,还是在思考它?我对数据的最佳方法是什么?
感谢 菲利普
答案 0 :(得分:1)
这应该让你开始。
让我们说你的数据是这样的
将此代码粘贴到模块中。 (注意:此代码未经过广泛测试,但传达了信息)
Option Explicit
Sub Sample()
Dim MyAr As Variant
Dim FinalAr() As String, TmpAr() As String
Dim ws As Worksheet
Dim lrow As Long, i As Long, n As Long, j As Long
'~~> Set this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> get last row of col A
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Store the values in an array
MyAr = .Range("A1:A" & lrow)
'~~> Loop through the array and split it on "&" and store it in another array
For i = LBound(MyAr) To UBound(MyAr)
If InStr(1, MyAr(i, 1), "&") Then
TmpAr = Split(MyAr(i, 1), "&")
For j = LBound(TmpAr) To UBound(TmpAr)
n = n + 1
ReDim Preserve FinalAr(n)
FinalAr(n) = Trim(TmpAr(j))
Next j
Else
n = n + 1
ReDim Preserve FinalAr(n)
FinalAr(n) = Trim(MyAr(i, 1))
End If
Next i
'~~> Past the outcome in Col B
.Range("B1").Resize(UBound(FinalAr) + 1, 1).Value = Application.Transpose(FinalAr)
'~~> Replace all mrs/mr etc
.Columns(2).Replace What:="MRS", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Columns(2).Replace What:="MR", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Columns(2).Replace What:="MISS", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'~~> Find Last Row of Col B
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
'~~> Loop through col B and split the names
For i = 2 To lrow
If InStr(1, .Range("B" & i), " ") Then
TmpAr = Split(Trim(.Range("B" & i)), " ")
n = 1
For j = LBound(TmpAr) To UBound(TmpAr)
.Range("B" & i).Offset(, n).Value = TmpAr(j)
n = n + 1
Next
Else
.Range("C" & i).Value = .Range("B" & i).Value
End If
Next i
End With
End Sub
OutCome(截图)