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

时间:2016-12-09 09:34:53

标签: excel vba excel-vba excel-formula

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

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

  • 洛杉矶纽约硅谷
  • Consumer InternetMobileB2BEnterprise SoftwareE-CommerceMarketplacesSocial

Let s call Los Angeles an item which is merged with another item New York我希望将这些项目分开,以便信息可以这样读取:

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

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

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

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

3 个答案:

答案 0 :(得分:2)

" 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 32)和(asc 45)之前没有空格或-,它会在答案中写入带空格的逗号。

修改: 解决方法SaaS和B2B 想法是引入一个逃脱符号。因此,每当我们看到&#34; \&#34;我们忽略它。此转义符号是通过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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

请将此代码粘贴到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

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