在excel中拆分地址,城市,州和邮政编码,缺少分隔符

时间:2010-09-21 22:11:26

标签: excel vba street-address

我有一个excel电子表格,其中包含整个地址,这些地址包含在没有分隔符的单个单元格中。地址如下所示:

2701 NW 64TH TER MARGATE FL 33063-1703 
901 NE 8 ST HALLANDALE BEACH FL 33009-2626 
1840 DEWEY ST UNIT 305 HOLLYWOOD FL 33020 
3049 NE 4 AVE WILTON MANORS FL 33334-2047
650 NE 56 CT OAKLAND PARK FL 33334-3528 

因此,A列中的前五个单元格将包含上述地址。

正如您所看到的,一些城市由两个单词组成,但州一直是FL或NY。我需要做的就是在他们自己的列中分隔地址,城市,州和邮政编码。我希望在excel中有一种方法可以在VBD(Visual Basic for Developers)中执行此操作。所以我可以将它放入一个宏。

我知道如何做到这一点,但我的VBD是有限的:

stateArray = Split("FL, NY")
cityArray = Split("Fort Lauderdale","Sunrise","Oakland Park")

例如,您可以使用另一种编程语言:

var arrStates, arrCities
arrCities = ["Fort Lauderdale", "Sunrise", "Oakland Park"]
arrStates = ["FL", "NY"]

var findAddress = function(curCity, curState){
    for(var i=0; i < arrCities.length; i < arrStates.length; i--){

        (arrCities[i] == curCity) ? arrCities[i] = CurCity : arrCities[i] = null;
        (arrStates[i] == curState) ? arrStates[i] = curState : arrStates[i] = null;

    }   

    if(arrCities[i] >= 0){
        var city = arrCities[i];
    }

    if(arrStates[i] >= 0){
        var state = arrStates[i];
    }

    createTable(city, state);

}

var createTable = function(city, state){
    var tbl = document.createElement("Table");
    var newRow = document.createElement("tr");
    tbl.appendChild(newRow);
    cols = [city, state];

    for(var i=0; i < cols.length; i++){
        var newCol = document.createElement("td");
        newCol.innerText = cols[i];
        newRow.appendChild(newCol);
    }
}

感谢您的回复。

3 个答案:

答案 0 :(得分:1)

似乎如果您必须输入所有城市,您可能只需手动拆分所有单元格。识别所有街道类型并将其用作分隔符可能更容易。注意数组中字符串周围的空格。

Sub SplitAddresses()

    Dim vaStates As Variant
    Dim vaStreets As Variant
    Dim i As Long
    Dim rCell As Range
    Dim sAddress As String
    Dim sCity As String, sState As String
    Dim sZip As String
    Dim lStreetPos As Long, lStatePos As Long

    vaStates = Array(" FL ", " NY ")
    vaStreets = Array(" TER ", " ST ", " AVE ", " CT ")

    For Each rCell In Sheet1.Range("A1:A5").Cells
        sAddress = "": sCity = "": sZip = "": sState = ""
        For i = LBound(vaStreets) To UBound(vaStreets)
            lStreetPos = InStr(1, rCell.Value, vaStreets(i))
            If lStreetPos > 0 Then
                sAddress = Trim(Left$(rCell.Value, lStreetPos + Len(vaStreets(i)) - 1))
                Exit For
            End If
        Next i

        For i = LBound(vaStates) To UBound(vaStates)
            lStatePos = InStr(1, rCell.Value, vaStates(i))
            If lStatePos > 0 Then
                sCity = Trim(Mid$(rCell.Value, Len(sAddress) + 1, lStatePos - Len(sAddress) - 1))
                sState = Trim(Mid$(rCell.Value, lStatePos + 1, Len(vaStates(i)) - 1))
                sZip = Trim(Mid$(rCell.Value, lStatePos + Len(vaStates(i)), Len(rCell.Value)))
                Exit For
            End If
        Next i

        rCell.Offset(0, 1).Value = "'" & sAddress
        rCell.Offset(0, 2).Value = "'" & sCity
        rCell.Offset(0, 3).Value = "'" & sState
        rCell.Offset(0, 4).Value = "'" & sZip

    Next rCell

End Sub

答案 1 :(得分:1)

在这样的情况下,我试着忘记我正在编程,只想想我会用什么样的思维过程来弄清楚每个人的正常情况。

2701 NW 64TH TER MARGATE FL 33063-1703 
901 NE 8 ST HALLANDALE BEACH FL 33009-2626 
1840 DEWEY ST UNIT 305 HOLLYWOOD FL 33020 
3049 NE 4 AVE WILTON MANORS FL 33334-2047
650 NE 56 CT OAKLAND PARK FL 33334-3528 

第一件事似乎很忙,但是如果你仔细观察就有模式。

  1. 地址全部以一组#s作为街道地址的起点
  2. 开头
  3. 街道地址始终以街道类型缩写
  4. 结尾
  5. 如果建筑物的某个单元位于街道地址
  6. 之后
  7. 地址全部以邮政编码
  8. 结尾
  9. 在邮政编码是州名缩写
  10. 之前
  11. 如果其他一切都被剥夺了,那么城市的名字就会在中间点亮。
  12. 这使得模式如下

    Street # : Street Type : Unit {Optional} : City : State: Zip Code
    

    使用字符串函数从临时字符串变量中删除每个部分,您应该能够非常轻松地重建它。

    希望有所帮助。

答案 2 :(得分:0)

以下是一些可以帮助您入门的VBA代码:您需要添加错误处理

Option Explicit
Option Compare Text
Sub SplitAddress()
    Dim vStates As Variant
    Dim vCities As Variant
    Dim vInput As Variant
    Dim vAddress() As Variant
    Dim j As Long
    Dim str1 As String

    ' States/Cities/Inputs are named ranges containing the data
    vStates = [States]
    vCities = [Cities]
    vInput = [Inputs]

    ReDim vAddress(1 To UBound(vInput) - LBound(vInput) + 1, 1 To 4)
    For j = 1 To UBound(vInput)
        str1 = Trim(CStr(vInput(j, 1)))
        If Len(str1) = 0 Then Exit For
        FindSplit j, 3, str1, vStates, vAddress()
        FindSplit j, 2, str1, vCities, vAddress()
    Next j

    ActiveSheet.Range("A2").Resize(UBound(vAddress), UBound(vAddress, 2)) = vAddress
End Sub
Sub FindSplit(j As Long, k As Long, str1 As String, vItems As Variant, vAddress() As Variant)
    Dim iPos As Long
    Dim jItem As Long
    Dim strItem As String

    For jItem = 1 To UBound(vItems)
        strItem = Trim(CStr(vItems(jItem, 1)))
        iPos = InStr(str1, " " & strItem & " ")
        If iPos > 0 Then
            vAddress(j, k) = Mid(str1, iPos + 1, Len(strItem))
            If k = 3 Then
                vAddress(j, k + 1) = Right(str1, Len(str1) - (iPos + 3))
                str1 = Left(str1, iPos)
            Else
                vAddress(j, 1) = Left(str1, iPos - 1)
            End If
            Exit For
        End If
    Next jItem
End Sub