如何在地址中添加逗号分隔符到城市的开头

时间:2015-05-29 14:37:26

标签: excel cell delimiter comma city

示例:

Philadelphia

我想在200个列表中的每个城市的开头添加一个逗号,但正如您在上面所看到的,有时城市名称不会从单元格中的最后一个单词开始。

是否有一个可以在Plymouth Meeting==之前添加逗号的总体公式?

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子,您可以适应您的使用。假设我们在 A 列中有地址,在 B 列中有城市列表:

enter image description here

以下宏扫描寻找 [空格] [城市] 的地址,并用 [,] [city] 替换城市

Sub Commafication()
   Dim Acol As String, Ccol As String
   Dim ia As Long, ic As Long, va As String, vc As String

''''''''''''''''''''''''''''''''''''
'  update these as needed
Acol = "A"
Ccol = "B"
iamax = 3
icmax = 6
''''''''''''''''''''''''''''''''''''

   For ia = 1 To iamax
      va = Cells(ia, Acol).Text
      For ic = 1 To icmax
         vc = Cells(ic, Ccol).Text
         If InStr(va, " " & vc) > 0 Then
            Cells(ia, Acol).Value = Replace(va, vc, "," & vc)
         End If
      Next ic
   Next ia
End Sub

结果如下:

enter image description here

<强>

这会根据您的要求将逗号放在城市名称之前,而不是在逗号和城市之间放置空格。