简体   繁体   English

使用后缀重新排序名称的各个部分

[英]Reorder parts of a name with a suffix

I need help on vba code to reorder the parts of a name.我需要 vba 代码的帮助来重新排序名称的各个部分。 Sometimes there is a suffix (Jr, Sr, I, II, III, IV), and that is the part I can't figure out.有时有一个后缀(Jr、Sr、I、II、III、IV),那是我想不通的部分。 There is no list that I need to loop thru.没有我需要遍历的列表。 The elements of the name could look like this: Johnson, Joseph Allen Jr名称的元素可能如下所示:Johnson, Joseph Allen Jr
This code works for getting the last name moved to the end, but now I need to trim & move the suffix to the right after the last name.此代码用于将姓氏移到末尾,但现在我需要修剪并将后缀移到姓氏之后的右侧。

Range("A1") = Trim(StrReverse(Split(StrReverse(Range("A1")), ",")(0)) & " " _
                    & StrReverse(Split(StrReverse(Range("A1")), ",")(1)))
Result:   Joseph Allen Jr    Johnson
Result Required:  Joseph Allen Johnson Jr

Thanks for any help!谢谢你的帮助!

So if your inputs are like Johnson, Joseph Allen Jr then you could set up an array/collection with the suffixes you want to check against.因此,如果您的输入类似于 Johnson、Joseph Allen Jr,那么您可以使用要检查的后缀设置一个数组/集合。 Then before you move the last name use然后在您移动姓氏之前使用

Dim i as Variant
Dim suffixArray as String()
Dim nameString as String
Dim suffixString as String
Dim arrayEntryLength as Long

suffixArray(0) = "Jr"
suffixArray(1) = "Sr"
suffixArray(2) = "I"
suffixArray(3) = "II"
...

for each i in suffixArray
    nameString = "Johnson, Joseph Allen Jr"    'or use Cells(1,"a").value
    arrayEntryLength = len(suffixArray(i))
    if Right(nameString, arrayEntryLength) = suffixArray(i) then
        suffixString = suffixArray(i)
        nameString = left(nameString, len(nameString)-arrayEntryLength)
    end if
next i

'move the last name of nameString

nameString = nameString & " " & suffixString

'rest of code

The full name is a unparsed string and manipulating "Suffix" as mentioned above by Chris H may be a suitable way.全名是一个未解析的字符串,像上面提到的 Chris H 操作“后缀”可能是一种合适的方式。 Here is Fullname content model in Outlook contact Item: https://docs.microsoft.com/en-us/office/vba/api/outlook.contactitem.fullname .这是 Outlook 中的全名内容 model 联系人项目: https://docs.microsoft.com/en-us/office/vba.api/outlook Building an array of known suffixes will solve this in general.构建一组已知后缀通常可以解决这个问题。 Watch out: the suffix may such as King Louis XVII, etc. is some historical context.注意:后缀可能如国王路易十七等是一些历史背景。

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

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