用分组词语标识大写字母并插入逗号和空格

我正在做一个任务,我必须从网站的内容复制/粘贴到Excel中。

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

  • 洛杉矶纽约硅谷
  • 消费者InternetMobileB2B企业软件电子商务市场社交

Let s call Los Angeles an item which is merged with another item New York ,我想分开这些项目,使信息是这样的可读性:

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

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

现在请帮助我知道有一个聪明的方法来解决这个问题,因为有一百个条目。 我所看到的是这个问题是如何解决的:

  1. 确定一个不在空格之后并且在其之前有小写字母的大写字母。
  2. 在这个地方插入一个逗号和空格,然后继续剩下的string。

请随意阐述,如果这不起作用,如果有一个替代解决scheme。 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 ,然后给string。 该函数查找大的字母(在65到90之间),如果没有空格,或者在大字母(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模块中。

 Function AddSpaces(pValue As String) As String Dim xOut As String xOut = VBA.Left(pValue, 1) For i = 2 To VBA.Len(pValue) xAsc = VBA.Asc(VBA.Mid(pValue, i, 1)) If xAsc >= 65 And xAsc <= 90 Then xOut = xOut & "," & " " & VBA.Mid(pValue, i, 1) Else xOut = xOut & VBA.Mid(pValue, i, 1) End If Next AddSpaces = xOut End Function 

之后,转到您的电子表格,并input此公式=addspaces(A1)将公式复制到所有您想要更改的单元格。

您可以复制网站上的内容并将其粘贴到记事本中。 然后从记事本复制内容并粘贴到Excel中。