Excel 当有两个姓氏时,如何在两列中拆分名字和姓氏

Excel 当有两个姓氏时,如何在两列中拆分名字和姓氏,excel,vba,Excel,Vba,我需要拆分名字和姓氏,但只需要2列和2个姓氏,这会导致问题 下面是我的代码,它使用文本来表示列,但是如果某人有两个姓氏,我需要能够将这两个都放在姓氏列中,而不是制作三列 列(“A:A”)。选择 Selection.TextToColumns Destination:=范围(“A1”),数据类型:=xlDelimited_ TextQualifier:=xlDoubleQuote,continuedDelimiter:=True,Tab:=False_ 分号:=False,逗号:=False,空

我需要拆分名字和姓氏,但只需要2列和2个姓氏,这会导致问题

下面是我的代码,它使用文本来表示列,但是如果某人有两个姓氏,我需要能够将这两个都放在姓氏列中,而不是制作三列


列(“A:A”)。选择
Selection.TextToColumns Destination:=范围(“A1”),数据类型:=xlDelimited_
TextQualifier:=xlDoubleQuote,continuedDelimiter:=True,Tab:=False_
分号:=False,逗号:=False,空格:=True,其他:=False,字段信息_
:=数组(数组(1,2),数组(2,1)),TrailingMinusNumbers:=True
单元格。选择
Selection.Columns.AutoFit

如果您在可乐中只有名字,并且您假设>2件物品会将“额外”分配给姓氏,那么:

Dim rng As Range, c As Range, v, arr


On Error Resume Next  '<< ignore the error if there's no data in ColA
Set rng = ActiveSheet.Range("A:A").SpecialCells(xlCellTypeConstants)
On Error GoTo 0       '<< stop ignoring errors

If Not rng Is Nothing Then
    For Each c In rng.Cells
        v = Trim(c.Value)
        If InStr(v, " ") > 0 Then
            arr = Split(v, " ", 2) '<< split only on the first space
            c.Resize(1, 2).Value = Array(arr(0), arr(1)) '<< populate the first and last names
        End If
    Next c
End If
Dim rng As Range,c As Range,v,arr

在错误恢复下一步“你怎么知道他们是两个姓而不是两个名?”?