2017-10-10 341 views
1

我如何将第一张照片(整个地址)分成第二张照片(地址,城市,州,邮政编码)。我有超过一千个数据,所以请提出简单的方法?如何在Excel中将一列分成三列?

所有整个地址的格式为729鹌鹑小河驱动器,弗里斯科TX 75034

我需要为729鹌鹑小河驱动,市弗里斯科分裂地址,国家为TX和邮政编码为。

Whole Address

Split Address, city, state, zipcode 感谢

+0

答:有了很大的难度,因为Excel不支持正则表达式匹配。我投票不在Excel中执行此任务。 –

+1

这取决于你的数据是如何格式化的。请提供一些例子,你已经尝试 – serakfalcon

+0

@TimBiegeleisen我在下面两个例子使用一个'Split'方法或'RegEx'方法 – Tom

回答

1

如果你知道你的数据将永远是这个格式:

<address>, <city> <2-letter state> <5-digit zip code> 

然后,这是我能想到的最简单的方法:

<address>的公式:

= LEFT(A1,FIND(",",A1)-1) 

公式<city>

= MID(A1,FIND(",",A1)+2,LEN(A1)-FIND(",",A1)-10) 

公式<2-letter state>

= MID(A1,LEN(A1)-7,2) 

公式<5-digit zip code>

= RIGHT(A1,5) 

参见下面的例子。

enter image description here

+0

谢谢你这么多。它的做工精细 –

+0

@Nirmalkumar大这样处理!您可以通过在旁边放置复选标记来接受答案吗? – ImaginaryHuman072889

1

这可能与RegExp做,但不是搞清楚RegExp模式我用Split功能和一对数组。我认为地址在逗号前面。

Sub SplitAddress() 
    Dim Addresses As Variant, results As Variant, tmp As Variant 
    Dim i As Long, j As Long 

    ' Update for you range 
    With Sheet1 
     'Trick to get 1D array from range 
     Addresses = Application.Transpose(.Range(.Cells(2, "A"), .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, "A"))) 
    End With 

    ReDim results(1 To UBound(Addresses), 1 To 4) 

    For i = 1 To UBound(results, 1) 
     tmp = Split(Addresses(i), ",") 
     results(i, 1) = Trim(tmp(0)) 

     tmp = Split(Trim(tmp(1)), " ") 

     For j = LBound(tmp) To UBound(tmp) 
      results(i, j + 2) = Trim(tmp(j)) 
     Next j 
    Next i 

    ' Update for your destination 
    With Sheet1.Cells(2, "B") 
     Range(.Offset(0, 0), .Offset(UBound(results, 1) - 1, UBound(results, 2) - 1)).Value2 = results 
    End With 
End Sub 

更新与正则表达式

此方法使用RegExp分割你的字符串

Sub splitAddressRegEx() 
    Dim ReGex As Object 
    Dim Addresses As Range 
    Dim j As Long 
    Dim c, m 

    ' Update for your range 
    With Sheet1 
     Set Addresses = .Range(.Cells(2, "A"), .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, "A")) 
    End With 
    Set ReGex = CreateObject("VBScript.RegExp") 

    With ReGex 
     .Global = True 
     .IgnoreCase = True 
     .Pattern = "(.+?(?=,))|(\w+)" 
    End With 

    For Each c In Addresses 
     j = 1 
     If ReGex.Test(c.Value2) Then 
      For Each m In ReGex.Execute(c.Value2) 
       ' Update for your output 
       c.Offset(0, j).Value2 = m 
       j = j + 1 
      Next m 
     End If 
    Next c 

End Sub