2014-03-13 99 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 **: –