拆分非统一数据

时间:2015-10-16 02:24:40

标签: excel vba excel-vba

我想在一个单元格中分成三段数据。现在,我的单元格看起来像这样,包含所有不同的位置,电子邮件和电话号码:

enter image description here

我希望我的数据看起来像这样:

enter image description here

我正在寻找任何类型的Excel公式或VBA代码来实现这一目标。我想也许某种MID,LEFT,RIGHT和FIND的组合可行,但我似乎无法做到正确。

2 个答案:

答案 0 :(得分:0)

行。这些是长公式,所以最好复制并粘贴它们。

假设您的数据在单元格A2中开始。

在单元格B2中,输入以下公式:

=SUBSTITUTE(A2,MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-3)),99),"")

在单元格C2中,输入以下公式:

=SUBSTITUTE(MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-3))+1,99),MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-2)),99),"")

在单元格D2中,输入以下公式:

=SUBSTITUTE(MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-2))+1,99),MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1)),99),"")

在单元格E2中,输入以下公式:

=MID(A2,FIND("|",SUBSTITUTE(A2," ","|",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))-1))+1,99)

现在选择范围B2:E2并根据需要向下复制。

那就是它。

答案 1 :(得分:0)

VBA版本(A栏中的所有文字)

Option Explicit

Public Sub customSplit()
    Dim part1, part2, part3, cel As Range, v As Variant, i As Long

    With ActiveSheet.UsedRange
        v = .Columns("A:E")
        For i = 1 To ActiveSheet.UsedRange.Rows.Count
            If Len(v(i, 1)) > 0 Then
                part1 = InStr(v(i, 1), "@")
                If part1 > 0 Then
                    part2 = InStrRev(v(i, 1), " ", part1)
                    part3 = Split(Right(v(i, 1), Len(v(i, 1)) - part2))
                    If UBound(part3) = 3 Then
                        v(i, 2) = Left(v(i, 1), part2 - 1)
                        v(i, 3) = part3(0)
                        v(i, 4) = part3(1)
                        v(i, 5) = part3(2) & " " & part3(3)
                    End If
                End If
            End If
        Next
        With .Columns("A:E")
            .Value = v
            .AutoFit
        End With
    End With
End Sub

总行数:1,048,576;持续时间:13.625秒

示例结果:

Associate Professor jane.doe@example.com 777-123-4567 GWC 123            Associate Professor            jane.doe@example.com   777-123-4567   GWC 123
Business Operation Specialist john.doe@example.com 777-456-7890 GWC 456  Business Operation Specialist  john.doe@example.com   777-456-7890   GWC 456
Associate Professor jane.doeexample.com 777-123-4567 GWC 124
Business Operation Specialist john.doe@example.com 777-456-7890 457
Associate Professor jane.doe@example.com 777-123-4567 GWC 125            Associate Professor            jane.doe@example.com   777-123-4567   GWC 125
Associate Professor jane.doe@example.com 777-123-4567 GWC 124            Associate Professor            jane.doe@example.com   777-123-4567   GWC 124
Business Operation Specialist john.doe@example.com 777-456-7890 GWC 457  Business Operation Specialist  john.doe@example.com   777-456-7890   GWC 457