简体   繁体   English

excel公式有助于拆分复杂名称

[英]excel formula help to split complex names

I have been tasked with some string manipulation and today must be my bad head day as it is proving more difficult than I expected. 我被要求进行一些字符串操作,今天一定是我头疼的日子,因为事实证明这比我预期的要困难得多。

I have to take the initials of the first and second and third name from the first and second and third columns along with any surnames 我必须从第一,第二和第三列中提取第一,第二和第三名称的缩写以及所有姓氏

Plus we need to keep the title. 另外,我们需要保留标题。

Here is an example of the long name as it stands now: 这是现在的长名称示例:

Mr C Chrysostomou & Mr N Chrysostomou & Mrs A Chrysostomou C Crysostomou先生和N N Chrysostomou先生及A Chrysostomou夫人

Mrs M Karseras & Ms P Hadjisoteriou & Mrs E Athanasiou M Karseras女士&P Hadjisoteriou女士&E Athanasiou女士

Mrs A Theodorou & Mr A Aristotelou & Mrs G Naziri & M Karmiou Mrs L Vazanias & Mrs G 西奥多鲁夫人(Ms A Theodorou)和亚里士多德(Aristotelou)夫人,纳兹里(G Naziri)夫人和卡米欧(Karmiou)M女士

Braithwaite & Mrs Helen West Mrs L Vazanias & Mrs G Braithwaite & Mrs Braithwaite和太太Helen West太太L Vazanias和太太G Braithwaite和太太

Helen West Mrs Olympia Pieridou & Mrs T&mr M & Mr C & Mrs K 海伦·韦斯特(Helen West)女士奥林匹亚·皮里杜(Olympia Pieridou)女士和T&mr女士M和C先生&K女士

Michaelides Miss JA Santamas& Mrs MT Santama- Solomonides& Mrs Lida Michaelides小姐JA Santamas女士和MT Santama女士-Solomonides女士和Lida女士

Santama Miss JA Santamas& Mrs MT Santama- Solomonides& Mrs Lida 桑塔玛JA Santamas小姐和MT桑塔玛夫人-Solomonides&Lida夫人

Santama Mr Polydoros Polydorou & Mrs Maro Themistocleous & Mrs Sylvia Santama Mr Polydoros Polydorou和Maro Themistocleous夫人&Sylvia夫人

Polydorou Mr Themis & Mrs Androulla & Mr Nicholas & Mrs Vasso Gina Polydorou先生Themis和Androulla夫人&Nicholas先生&Vasso Gina夫人

Demetriou Mrs SK Makkofaides & Mr Z Koullas & Mrs Y Koullas & Mrs R Demetriou SK Makkofaides夫人,Z Koullas夫人,Y Koullas夫人和R夫人

Kleopa Mr G Zorzy & Mrs H Louca Zorzy & Mr S Stavropoulos & Mrs Y Kleopa先生G Zorzy女士和H Louca Zorzy女士&S Stavropoulos先生&Y女士

Stavropoulos Mrs M Franceschina & Ms C Eugeniou & Ms OL Toumazides Stavropoulos女士Franceschina女士&C Eugeniou女士&OL Toumazides女士

T/a The Three Cupcakes Mr David & Mrs Eileen Nixon Dhnixon & Co. - Office Account T / a戴维先生和艾琳·尼克松夫人(Dhnixon&Co.)三杯蛋糕-办公室帐户

as you can see, these could be considered to be joint bank accounts between 2 or even 3 persons. 如您所见,这些可以被视为2人甚至3人之间的联合银行帐户。 we will have to keep the tite, which could be Mr, Miss, Ms, Dr, Doctor , or Messrs along with the initials of the first and second names and the full surname, and the total should be less than 35 characters ! 我们必须保持礼节,可能是先生,小姐,女士,博士,医生先生,以及名字的首字母缩写和全名,总数应少于35个字符!

so, here is what I have been trying after some searching on the web: 所以,这是我在网上搜索后一直在尝试的方法:

=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),"")

that gets the initials, but only the first 2 得到首字母缩写,但只有前2个

=RIGHT(J:J,LEN(J:J)-FIND(" ",J:J)+1)

gets the surname but isn't working correctly. 获得姓氏,但工作不正常。

am I over thinking this, or under thinking it? 我是在想这个,还是在想呢?

What is my best approach to the data? 我最好的数据处理方法是什么?

thanks Philip 谢谢菲利普

This should get you started. 这应该使您入门。

Lets say your data looks like this 可以说您的数据如下所示

在此处输入图片说明

Paste this code in a module. 将此代码粘贴到模块中。 (Note: This code is not extensively tested but conveys the message) (注意:此代码尚未经过广泛测试,但可以传达信息)

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 (Screenshot) OutCome(屏幕截图)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM