2014-03-13 67 views
0

我需要将具有地址列设置的Excel数据库设置为: “Physical Address,Mailing Address,Suit#; City; ST; Zip”分隔为分号分隔的列。拆分地址列

不幸的是,我有地址列中有ASCII字符引用,不允许我只使用“文本到列”的数据,所以我开发了下面的代码,但它并没有做我想要的东西做。我是分割的范围是B列

Sub SplitAddress() 
    Dim txt As String 
    Dim i As Integer 
    Dim j As Integer 
    Dim Address As Variant 
    Dim Rng As Range 
    Dim Row As Range 
    Dim LastRow As Integer 

    txt = ActiveCell.Value 
    Address = Split(txt, "; ") 

    LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row 
    Rng = Range("B3:B" & LastRow) 

    j = 1 

    For Each Row In Rng.Rows 
     For i = 0 To UBound(Address) 
      Cells(3, j + 1).Value = Address(i) 
     Next i 
    Next Row 
End Sub 

回答

1

也许:

Sub SplitAddress() 
    Dim txt As String 
    Dim i As Integer 
    Dim j As Integer 
    Dim Address As Variant 
    Dim Rng As Range 
    Dim R As Range 
    Dim LastRow As Integer 
    LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row 
    Set Rng = Range("B3:B" & LastRow) 
    For Each R In Rng 
     txt = R.Value 
     Address = Split(txt, "; ") 
     j = R.Row 
     For i = 0 To UBound(Address) 
      Cells(j, i + 3).Value = Address(i) 
     Next i 
    Next R 
End Sub 

编辑#1

更好地使I,J,LASTROW龙而非整数

Sub SplitAddress() 
    Dim txt As String 
    Dim i As Long 
    Dim j As Long 
    Dim Address As Variant 
    Dim Rng As Range 
    Dim R As Range 
    Dim LastRow As Long 
    LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row 
    Set Rng = Range("B3:B" & LastRow) 
    For Each R In Rng 
     txt = R.Value 
     Address = Split(txt, "; ") 
     j = R.Row 
     For i = 0 To UBound(Address) 
      Cells(j, i + 3).Value = Address(i) 
     Next i 
    Next R 
End Sub 

编辑#2

这个版本移动结果到,从而过度写入列

Sub SplitAddress() 
    ' version #3 - overwrites column B 
    Dim txt As String 
    Dim i As Long 
    Dim j As Long 
    Dim Address As Variant 
    Dim Rng As Range 
    Dim R As Range 
    Dim LastRow As Integer 
    LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row 
    Set Rng = Range("B3:B" & LastRow) 
    For Each R In Rng 
     txt = R.Value 
     Address = Split(txt, "; ") 
     j = R.Row 
     For i = 0 To UBound(Address) 
      Cells(j, i + 2).Value = Address(i) 
     Next i 
    Next R 
End Sub 
+0

我得到溢出错误... –

+0

在其中line?..................... –

+0

看到我的**编辑#1 **: –