2

Excel には、"FirstName LastName" という形式の名前の列があります。その列全体を 2 つの列に分割し、1 つはすべての名前を含み、もう 1 つはすべての姓を含みます。

これまでの私のコード:

    'Splitting the Traveler Display Name column
    Dim SplitPoint As Long
    'L2 is the column containing names to be split
    Range("L2").Select
    Do Until IsEmpty(ActiveCell)
        'Search for position of space within the cell
        SplitPoint = InStrRev(ActiveCell, " ", -1, vbTextCompare)
        'Put the last name in the column next to the source column
        ActiveCell.Offset(0, 1) = Trim(Left(ActiveCell, SplitPoint))
        'Replace the source column with the first name
        ActiveCell.Offset(0, 0) = Trim(Mid(ActiveCell, SplitPoint))
    Loop

これまでに見つけた解決策では、セルを手動で選択する必要がありましたが、これは私が扱っているデータの量には不合理でした。この解決策を見つけましたが、次のエラーが表示されます: Invalid Procedure call or argument

4

2 に答える 2

5

NON VBA メソッド

Data~~>Text To Columns を使用しないのはなぜですか?

ここに画像の説明を入力

VBA メソッド

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim tmpArray() As String

    '~~> This is the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        LastRow = .Range("L" & .Rows.Count).End(xlUp).Row

        For i = 2 To LastRow
            If InStr(1, .Range("L" & i).Value, " ") Then
                tmpArray = Split(.Range("L" & i).Value, " ")
                .Range("M" & i).Value = tmpArray(0)
                .Range("N" & i).Value = tmpArray(1)
            End If
        Next i
    End With
End Sub
于 2013-04-03T16:24:44.613 に答える