这是我几年前写的东西,所以我不确定其中是否存在错误,但快速测试似乎表明它工作正常。您可能必须对其进行更改以使其完全适合您的情况。
代码:
Option Explicit
Sub SplitAddress()
Dim MyAr() As String, tempStr As String, strUnique As String
Dim lRow As Long, i As Long, j As Long, lRow2 As Long
Dim cell As Range
strUnique = "SiddR" & Format(Now, "ddmmyyhhmmss")
With ActiveSheet
.Columns("A:A").Replace What:=" ", Replacement:=strUnique, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Columns("C").NumberFormat = "@"
.Columns("D").NumberFormat = "@"
For i = 2 To lRow
MyAr = Split(.Range("A" & i).Value, strUnique)
tempStr = ""
For j = LBound(MyAr) To (UBound(MyAr) - 1)
If tempStr = "" Then
tempStr = MyAr(j)
Else
tempStr = tempStr & " " & MyAr(j)
End If
Next j
.Range("B" & i).Value = tempStr
.Range("C" & i).Value = MyAr(UBound(MyAr))
Next i
For i = 2 To lRow
If Not IsNumeric(.Range("C" & i).Value) Then
tempStr = ""
For j = 1 To Len(.Range("C" & i).Value)
If IsNumeric(Mid(.Range("C" & i).Value, j, 1)) Then
If tempStr = "" Then
tempStr = Mid(.Range("C" & i).Value, j, 1)
Else
tempStr = tempStr & Mid(.Range("C" & i).Value, j, 1)
End If
Else
Exit For
End If
Next
.Range("D" & i).Value = Mid(.Range("C" & i).Value, j)
.Range("C" & i).Value = tempStr
If Len(Trim(tempStr)) = 0 Then
MyAr = Split(.Range("A" & i).Value, strUnique)
.Range("C" & i).Value = MyAr(UBound(MyAr) - 1)
End If
End If
Next
.Columns("A:A").Replace What:=strUnique, Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Columns("D:D").Replace What:="-", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
End Sub
截图:
截图:
使用您的测试数据
编辑:现在当我再次查看这段代码时,我发现它可以进一步优化:)