gpt4 book ai didi

vba - 识别分组单词中的大写字母并插入逗号和空格

转载 作者:行者123 更新时间:2023-12-01 18:00:27 26 4
gpt4 key购买 nike

我正在执行一项任务,必须将网站内容复制/粘贴到 Excel 中。

但问题是当我复制/粘贴 Excel 中的内容时,它看起来像这样:

  • 洛杉矶纽约硅谷
  • 消费者互联网移动B2B企业软件电子商务市场社交

让我们将洛杉矶称为与纽约的另一个项目合并的项目,我想将这些项目分开,以便信息可读,如下所示:

  • 洛杉矶、纽约、硅谷
  • 消费者互联网、移动、B2B、企业软件、电子商务、市场、社交

当我注意到时,我实际上意识到在网站上(由于某些技术原因)我无法复制项目之间的逗号,因此所有其他项目都与前一个项目以大写字母合并。

现在请帮助我知道是否有一种智能方法可以解决这个问题,因为有数百个条目。我看到的是这个问题的解决方法:

  1. 识别一个大写字母,该字母后面没有空格并且前面有小写字母。
  2. 在该位置插入逗号和空格,然后继续处理剩余的字符串。

如果这不起作用以及是否有替代解决方案,请随时详细说明。 VBA 代码/Excel 公式 - 任何可以帮助我实现自动化的东西。谢谢。

最佳答案

使用“B2B”会有点困难,但它与其他方法配合得很好:

Public Sub TestMe()

Debug.Print insert_a_space("Los AngelesNew YorkSilicon Valley")
Debug.Print insert_a_space("Consumer InternetMobileB2BEnterprise SoftwareE-CommerceMarketplacesSocial")

End Sub

Public Function insert_a_space(my_str As String)

Dim my_char As String
Dim l_counter As Long
Dim str_result As String

For l_counter = 1 To Len(my_str)
my_char = Mid(my_str, l_counter, 1)

If Asc(my_char) >= 65 And Asc(my_char) <= 90 Then
If l_counter > 1 Then
If Asc(Mid(my_str, (l_counter - 1), 1)) <> 32 And _
Asc(Mid(my_str, (l_counter - 1), 1)) <> 45 Then
str_result = str_result & ", "
End If
End If
End If
str_result = str_result & my_char
Next l_counter
insert_a_space = str_result

End Function

逻辑是您运行TestMe。或者用作 Excel 函数 insert_a_space,然后给出字符串。该函数查找大字母(介于 65 和 90 asc 之间),如果大字母 (asc 32) 和 (asc 45) 之前没有空格或 -,则会在其中写入一个带空格的逗号答案。

编辑:SaaS 和 B2B 解决方法这个想法是引入一个转义符号。因此,每当我们看到“\”时,我们都会忽略它。这个转义符号是通过 str_replace_me 引入的,应该明确地写出它是哪个选项。

Public Sub TestMe()

Dim str_1 As String
Dim str_2 As String

str_1 = "Los AngelesNew YorkSilicon Valley"
str_2 = "Consumer InternetMobileB2BEnterprise SoftwareE-CommerceMarketplacesSocialSaaS"

Debug.Print insert_a_space(str_replace_me(str_1))
Debug.Print insert_a_space(str_replace_me(str_2))

End Sub

Public Function str_replace_me(my_str As String) As String

str_replace_me = Replace(my_str, "SaaS", "Saa\S")
str_replace_me = Replace(str_replace_me, "B2B", "B2\B")

End Function

Public Function insert_a_space(my_str As String)

Dim my_char As String
Dim l_counter As Long
Dim str_result As String

For l_counter = 1 To Len(my_str)
my_char = Mid(my_str, l_counter, 1)

If Asc(my_char) >= 65 And Asc(my_char) <= 90 Then
If l_counter > 1 Then
If Asc(Mid(my_str, (l_counter - 1), 1)) <> 32 And _
Asc(Mid(my_str, (l_counter - 1), 1)) <> 45 And _
Asc(Mid(my_str, (l_counter - 1), 1)) <> 92 Then

str_result = str_result & ", "

End If
End If
End If
str_result = str_result & my_char
Next l_counter


str_result = Replace(str_result, "\", "")
insert_a_space = str_result

End Function

关于vba - 识别分组单词中的大写字母并插入逗号和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41057199/

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