VBA交换字符串中的单词

时间:2019-03-14 15:04:43

标签: excel vba

在VBA中,我做了一个用户表格。它包含多个文本框,用户可以在其中输入文本。在一个文本框中,用户应输入其姓氏。我做了一个名为lastname的变量,然后做了lastname = LastnameBox.Value

我的问题是:
例如,如果有人输入de Vries,我该如何在Vries, de中进行更改。或者,如果有人键入van de Voort van Zijp,则需要在Voort van Zijp, van de中进行更改。

如何在VBA中实现这一目标?

3 个答案:

答案 0 :(得分:3)

我会尝试以下方法。不确定您如何要求分隔,我使用“ de”

    Function NamesTest(strNameIn As String)

Dim a() As String

a = Split(strNameIn, "de")

a(0) = a(0) & " de"

NamesTest = a(1) & "," & a(0)

End Function

答案 1 :(得分:0)

这里有两个选择。第一个将拿起最后一个单词并进行交换。它不注意字母大小写。

Sub LastFirst()

    Debug.Print RevLast("de Vries")
    Debug.Print RevLast("van der Straat")
    Debug.Print RevLast("van de drake")

End Sub
Function RevLast(Name)

    LastName = Trim(Right(Replace(Name, " ", String(99, " ")), 99))
    LenLastName = Len(LastName)
    FirstPart = Left(Name, Len(Name) - (LenLastName + 1))
    RevLast = LastName + ", " + FirstPart

End Function

仅有的第二个交换是大写字母。

Sub UppercaseFirst()

    Name = "de Vries"
    Name = "van der Straat"
    Debug.Print RevUpper("de Vries")
    Debug.Print RevUpper("van der Straat")
    Debug.Print RevUpper("van de drake")

End Sub

Function RevUpper(Name)

    FirstUpper = -1
    On Error Resume Next
    xStr = Trim(Rg.Value)
    For j = Len(Name) To 1 Step -1
        If (Asc(Mid(Name, j, 1)) < 91) And (Asc(Mid(Name, j, 1)) > 64) Then
            FirstUpper = Len(Name) - j + 1
            Exit For
        End If
    Next

    If FirstUpper > 0 Then
        LastName = Right(Name, FirstUpper)
        FirstPart = Left(Name, Len(Name) - (FirstUpper + 1))
        NewName = LastName + ", " + FirstPart
        RevUpper = NewName
    Else
        RevUpper = "Invalid"
    End If

End Function

Function RevNm(Name)

    FirstUpper = -1
    On Error Resume Next
    xStr = Trim(Rg.Value)
    For j = Len(Name) To 1 Step -1
        If (Asc(Mid(Name, j, 1)) < 91) And (Asc(Mid(Name, j, 1)) > 64) Then
            FirstUpper = Len(Name) - j + 1
            Exit For
        End If
    Next

    If FirstUpper > 0 Then
        LastName = Right(Name, FirstUpper)
        FirstPart = Left(Name, Len(Name) - (FirstUpper + 1))
        NewName = LastName + ", " + FirstPart
        RevNm = NewName
    Else
        RevNm = "Invalid"
    End If


End Function

答案 2 :(得分:0)

这是标题中所述问题的更通用的解决方案(不处理将名字/姓氏反转的具体问题,这是一个不同的问题):

Public Function ReverseWords(ByVal value As String) As String

    Dim words As Variant
    words = VBA.Strings.Split(value, " ")

    Dim result As String, i As Long
    For i = LBound(words) To UBound(words)
        result = words(i) & " " & result
    Next

    ReverseWords = result

End Function

用法:

Debug.Print ReverseWords("the quick brown fox jumps over the lazy dog")
     

输出:

dog lazy the over jumps fox brown quick the 

不过,对于OP来说,这根本不是要反转字符串中的单词。解决方案是解析给定的字符串。

  

首个大写字母确实是我要交换的位置

因此,您需要在输入字符串中找到第一个大写字母的索引,然后提取名字和姓氏,对其进行修剪,然后将它们连接起来。

这有效:

Public Function ReverseFullName(ByVal value As String) As String

    Dim firstCapitalIndex As Long, i As Long
    For i = 1 To Len(value)
        If IsCapitalLetter(Mid$(value, i, 1)) Then
            firstCapitalIndex = i
            Exit For
        End If
    Next

    If i = 1 Then
        'already shaped as needed
        ReverseFullName = value
        Exit Function
    End If

    Dim firstName As String
    firstName = Trim$(Left$(value, firstCapitalIndex - 1))

    Dim lastName As String
    lastName = Trim$(Mid$(value, firstCapitalIndex))

    ReverseFullName = lastName & ", " & firstName

End Function

Private Function IsCapitalLetter(ByVal value As String) As Boolean
    Dim asciiCode As Integer
    asciiCode = Asc(value)
    IsCapitalLetter = asciiCode >= Asc("A") And asciiCode <= Asc("Z")
End Function

用法:

Debug.Print ReverseFullName("van de Voort van Zijp")
Debug.Print ReverseFullName("de Vries")
Debug.Print ReverseFullName("Voort van Zijp, van de")
     

输出:

Voort van Zijp, van de
Vries, de
Voort van Zijp, van de