Excel Formula: how to split string by capital letters

前端 未结 3 1816
执笔经年
执笔经年 2021-01-16 19:38

Using a formula, not VBA, I would like to come up with a solution to split a string composed of multiple words. The formula should recognize the words where there is a capit

3条回答
  •  既然无缘
    2021-01-16 20:24

    To accomplish this, you will need pure VBA. Create a custom Function to get in 1 cell the string you want. Then, use Text to Columns later if you need it.

    My function:

    Public Function GET_STRING(ByVal ThisCell As Range) As String
    Dim i As Integer
    
    Dim MyPositions As String
    Dim ArrPositions As Variant
    
    For i = 2 To Len(ThisCell.Value) Step 1
        If Mid(ThisCell.Value, i, 1) = UCase(Mid(ThisCell.Value, i, 1)) And _
        Mid(ThisCell.Value, i, 1) <> " " And Left(Mid(ThisCell.Value, i - 1, 1), 1) <> " " Then MyPositions = MyPositions & i & ";"
    Next i
    
    ArrPositions = Split(Left(MyPositions, Len(MyPositions) - 1), ";")
    
    For i = 0 To UBound(ArrPositions) Step 1
        If i = 0 Then
            GET_STRING = Left(ThisCell.Value, ArrPositions(i) - 1) & "," & Mid(ThisCell.Value, ArrPositions(i), ArrPositions(i + 1) - ArrPositions(i))
        ElseIf i <> UBound(ArrPositions) Then
            GET_STRING = GET_STRING & "," & Mid(ThisCell.Value, ArrPositions(i), ArrPositions(i + 1) - ArrPositions(i))
        Else
            GET_STRING = GET_STRING & "," & Mid(ThisCell.Value, ArrPositions(i), Len(ThisCell.Value) - ArrPositions(i) + 1)
        End If
    Next i
    
    End Function
    

    What I get when i use it on excel

提交回复
热议问题