将单元格的垂直文本拆分为多列

时间:2018-09-03 19:09:04

标签: excel

我有一个包含地址的Excel单元格。我希望将地址拆分为单独的单元格:

地址 市 州 邮编

这些项是垂直堆叠的,否则,我将只使用“数据”>“文本到列”:

enter image description here

更新:我已经尝试了定界符选项,但是我无法拆分所有行:

enter image description here

2 个答案:

答案 0 :(得分:2)

您很容易解决问题:)是的,Data-> Text to columns

诀窍在于,您可以使用 Ctrl + J 作为分隔符,这将键入new-line字符。

信用:https://trumpexcel.com/split-multiple-lines/

顺便说一句,对此的Google搜索字符串是excel split cells with multiple lines

答案 1 :(得分:0)

以下内容将在列中转换一个长列表。如果您只有一个联系信息单元,则最好手动进行。

Option Explicit

Sub gatherAddress()
    Dim i As Long, var As Variant, tmp As Variant

    With Worksheets("sheet2")
        'put the new header labels in the top row
        .Cells(1, "B").Resize(1, 4) = Array("Address", "City", "State", "Zip")

        'collect the values from column A
        var = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)).Value2

        'make room for processed values
        ReDim Preserve var(LBound(var, 1) To UBound(var, 1), _
                           LBound(var, 2) To UBound(var, 2) + 4)

        'process the address into separate cells
        For i = LBound(var, 1) To UBound(var, 1)
            'split the billing address on the linefeed
            tmp = Split(var(i, 1), vbLf)

            'strip address line
            var(i, 2) = tmp(1)
            'strip city
            var(i, 3) = Split(tmp(2), Chr(44))(0)
            'strip city and zip
            var(i, 4) = Split(tmp(2), Chr(44))(1)
            'strip zip/zip+4 code
            var(i, 5) = Split(var(i, 4), Chr(32))(UBound(Split(var(i, 4), Chr(32))))
            'remove zip from city
            var(i, 4) = Trim(Replace(var(i, 4), var(i, 5), vbNullString))
        Next i

        'put the processed values back into the worksheet
        .Cells(2, "A").Resize(UBound(var, 1), UBound(var, 2)) = var
    End With

End Sub

enter image description here