Excel公式:如何按大写字母分割字符串

问题描述 投票:0回答:4

使用公式,而不是VBA,我想提出一个解决方案来分割由多个单词组成的字符串。该公式应该识别有大写字母的单词并将它们分开。结果将是一个字符串,其中单词之间用“,”分隔。

为了澄清这一点,这是一个字符串的示例:

Nursing StudentStudentNurseNursing School

Desired Result:
Nursing Student,Student,Nurse,Nursing School

我正在尝试以下公式,但我只能隔离第一个单词:

{=LEFT(Q4,SMALL(FIND(CHAR(ROW(INDIRECT("65:90"))),Q4&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"),2)-1)}

有什么建议吗?

excel excel-formula
4个回答
3
投票

要完成此任务,您将需要纯 VBA。创建一个自定义函数以在 1 个单元格中获取所需的字符串。然后,如果需要,稍后可以使用“文本分列”。

我的功能:

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

当我在 Excel 上使用它时我得到了什么

enter image description here


2
投票

您的这一要求挑战了极限。您想要实现的目标需要重复循环同一字符串。这只能通过递归来完成,而 Excel 公式不执行递归。

使用现代 Excel 2016,您可以使用 Power Query(获取和转换,或 Excel 2010 和 2013 的加载项),如果您不想使用 VBA,则可以使用它以 M 代码编写逻辑。 Power Query 可以保存在无宏工作簿中,并且可以通过单击功能区中的“全部刷新”命令来处理新数据。


1
投票

这是我在access VBA中使用的 传递一个字符串,例如 ?GET_SPLIT_STRING("SplitAtCapitals") 并返回以下内容 在首都分裂

Public Function GET_SPLIT_STRING(xStr As String) As String
    Dim i As Integer, xchar As String, ychar As String
    ychar = UCase(Left(xStr, 1))
    For i = 2 To Len(xStr) Step 1
        xchar = Mid(xStr, i, 1)

        If Asc(xchar) = Asc(UCase(xchar)) Then
            xchar = Space(1) & xchar
        End If
        ychar = ychar & xchar
    Next
    GET_SPLIT_STRING = ychar
End Function

0
投票

在B2:C28中填写这些:

A   ,A
B   ,B
C   ,C
D   ,D
E   ,E
F   ,F
G   ,G
H   ,H
I   ,I
J   ,J
K   ,K
L   ,L
M   ,M
N   ,N
O   ,O
P   ,P
Q   ,Q
R   ,R
S   ,S
T   ,T
U   ,U
V   ,V
W   ,W
X   ,X
Y   ,Y
Z   ,Z
 ,   

注:B28 = , C28 =

然后在A2

=SUBSTITUTE(A1,B2,C2)
然后拖动直到A28,

在 A29

=RIGHT(A28,LEN(A28)-1)
完成。

希望有帮助。 (:

+------[编辑]-----+

或一行:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"Z",",Z"),"Y",",Y"),"X",",X"),"W",",W"),"V",",V"),"U",",U"),"T",",T"),"S",",S"),"R",",R"),"Q",",Q"),"P",",P"),"O",",O"),"N",",N"),"M",",M"),"L",",L"),"K",",K"),"J",",J"),"I",",I"),"H",",H"),"G",",G"),"F",",F"),"E",",E"),"D",",D"),"C",",C"),"B",",B"),"A",",A")," ,"," ")
© www.soinside.com 2019 - 2024. All rights reserved.