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

Vba 识别分组单词中的大写字母,并插入逗号和空格,vba,excel,excel-formula,Vba,Excel,Excel Formula,我正在执行一项任务,我必须将网站内容复制/粘贴到excel中 但问题是,当我在excel中复制/粘贴内容时,它显示如下: 洛杉矶纽约硅谷 消费者互联网手机B2B企业软件商业市场社交 让我们把洛杉矶称为一个项目,它与纽约的另一个项目合并,我想把这些项目分开,这样信息就可以像这样阅读: 洛杉矶、纽约、硅谷 消费者互联网、移动、B2B、企业软件、电子商务、市场、社交网络 当我注意到,我实际上意识到,在网站上,由于某些技术原因,我无法复制项目之间的逗号,因此每个其他项目都与上一个项目合并为大写字母 现在

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

但问题是,当我在excel中复制/粘贴内容时,它显示如下:

洛杉矶纽约硅谷 消费者互联网手机B2B企业软件商业市场社交 让我们把洛杉矶称为一个项目,它与纽约的另一个项目合并,我想把这些项目分开,这样信息就可以像这样阅读:

洛杉矶、纽约、硅谷 消费者互联网、移动、B2B、企业软件、电子商务、市场、社交网络 当我注意到,我实际上意识到,在网站上,由于某些技术原因,我无法复制项目之间的逗号,因此每个其他项目都与上一个项目合并为大写字母

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

标识一个大写字母,该字母不在空格后面,前面有小写字母。 在该位置插入逗号和空格,然后继续使用剩余的字符串。
请随时详细说明这是否可行,以及是否有其他解决方案。VBA代码/Excel公式-任何可以帮助我自动化它的东西。谢谢。

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

请将此代码粘贴到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
然后转到电子表格,输入以下公式=addspacesA1
将公式复制到您要更改的所有单元格。

对于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

谢谢你的VBA代码。但它是在每个大写字母前插入逗号。我想要的是当你找到一个大写字母并且前面没有空格时,插入一个逗号;在这种情况下,它会附上一封小信。例如:信息技术在线约会**应该改为**信息技术在线约会。请建议这将需要很多时间。另外,我还需要插入一些逗号。excel中没有实现这一点的方法吗。谢谢。它很有魅力。但也有一些小故障,如电视、B2B和SaaS。我们是否有可能从algo中排除某些单词,这样就不会影响它们。除此之外,它还能提供99%的输出。谢谢你的更新。您必须引入一个转义符号,它将与SaaS和B2B一起工作。还有电视。非常感谢你的支持。真是帮了大忙。再次感谢:
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