gpt4 book ai didi

vba - 在 Excel 中拆分大写单词并插入分隔符

转载 作者:行者123 更新时间:2023-12-04 19:55:56 25 4
gpt4 key购买 nike

我有这个字符串:

RugbyFunny RugbyGirls RugbyBoys RugbyWomens Rugby

基本上,我想用大写字母分割单词并放置一个像 ; 这样的分隔符。

我发现了一个有用的 VBA 函数,可以完成部分工作:

Function splitbycaps(inputstr As String) As String
Dim i As Long
Dim temp As String

If inputstr = vbNullString Then
splitbycaps = temp
Exit Function
Else
temp = inputstr
For i = 1 To Len(temp)
If Mid(temp, i, 1) = UCase(Mid(temp, i, 1)) Then
If i <> 1 Then
temp = Left(temp, i - 1) + " " + Right(temp, Len(temp) - i + 1)
i = i + 1
End If
End If
Next i
splitbycaps = temp
End If
End Function

如何在每个单词之间添加分隔符?我想产生这样的结果:

Rugby;Funny Rugby;Girls Rugby;Boys Rugby;Womens Rugby;

非常感谢您的帮助!

最佳答案

将函数更改为:

Function SplitByCaps(InputStr As String) As String
Dim i As Long
Dim temp As String

If InputStr = vbNullString Then
SplitByCaps = temp
Exit Function
Else
temp = InputStr
Do While i < Len(temp)
i = i + 1
If Mid(temp, i, 1) <> LCase(Mid(temp, i, 1)) Then
If i <> 1 Then
If Mid(temp, i - 1, 1) <> " " Then
temp = Left(temp, i - 1) & ";" & Right(temp, Len(temp) - i + 1)
i = i + 1
End If
End If
End If
DoEvents
Loop
SplitByCaps = temp
End If
End Function

编辑:将其更改为 Do 循环,正如 @Vityata 指出的那样,For 计数不正确。

Public Sub Test()
Dim str As String
str = "RugbyFunny RugbyGirls RugbyBoys RugbyWomens Rugby"

Debug.Print SplitByCaps(str)
'Rugby;Funny Rugby;Girls Rugby;Boys Rugby;Womens Rugby
End Sub

关于vba - 在 Excel 中拆分大写单词并插入分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com