Разделенные имена
Предположения
В ячейке указывается два или три имени (человек):
- Имя и Фамилия или
- Имя , Отчество и Фамилия .
Вы хотели, чтобы имена из одного столбца были разделены на три столбца.
Код
- Настройте значения в разделе констант в соответствии со своими потребностями.
- Вы можете выбрать ту же букву или цифру столбца, если хотите
перезаписать исходные данные, но сделать это после тестирования кода.
Sub SplitNames()
Const cSource As Variant = "A" ' Source Column Letter/Number
Const cTarget As Variant = "B" ' Target Column Letter/Number
Const cFirstR As Long = 2 ' Source/Target First Row Number
Dim vntS As Variant ' Source Array
Dim vntD As Variant ' Delimited Array
Dim vntT As Variant ' Target Array
Dim LastR As Long ' Source/Target Last Row Number
Dim i As Long ' Source/Target Array Row Counter
' Calculate Source/Target Last Row Number.
LastR = Cells(Rows.Count, cSource).End(xlUp).Row
' Copy Source Range into Source Array.
vntS = Range(Cells(cFirstR, cSource), Cells(LastR, cSource))
' Resize Target Array's rows to the number of rows in Source Array,
' but to three columns: First, Middle, Last.
ReDim vntT(1 To UBound(vntS), 1 To 3)
' Copy from Source Array to Target Array.
For i = 1 To UBound(vntS) ' Rows of Source/Target Array
vntD = Split(vntS(i, 1)) ' Split each row of Source Array.
vntT(i, 1) = vntD(0) ' First Name
If UBound(vntD) = 2 Then ' Does have middle name.
vntT(i, 2) = vntD(1) ' Middle Name
vntT(i, 3) = vntD(2) ' Last Name
Else ' Does not have middle name.
vntT(i, 3) = vntD(1) ' Last Name
End If
Next
' Copy Target Array into Target Range.
Range(Cells(cFirstR, cTarget), Cells(LastR, cTarget)) _
.Resize(UBound(vntT), UBound(vntT, 2)) = vntT
End Sub
Вторая версия
- Настройте значения в разделе констант в соответствии со своими потребностями.
- Вы можете выбрать ту же букву или цифру столбца, если хотите
перезаписать исходные данные, но сделать это после тестирования кода.
Sub SplitNames2()
Const cSource As Variant = "A" ' Source Column Letter/Number
Const cTarget As Variant = "B" ' Target Column Letter/Number
Const cFirstR As Long = 7 ' Source/Target First Row Number
Const cNum As Long = 3 ' Number of Words
Dim vntS As Variant ' Source Array
Dim vntD As Variant ' Delimited Array
Dim vntT As Variant ' Target Array
Dim LastR As Long ' Source/Target Last Row Number
Dim i As Long ' Source/Target Array Row Counter
Dim j As Long ' Delimited Array Rows Counter
' Calculate Source/Target Last Row Number.
LastR = Cells(Rows.Count, cSource).End(xlUp).Row
' Copy Source Range into Source Array.
vntS = Range(Cells(cFirstR, cSource), Cells(LastR, cSource))
' Resize Target Array's rows to Source Array,
ReDim vntT(1 To UBound(vntS), 1 To 1)
' Copy from Source Array to Target Array.
For i = 1 To UBound(vntS) ' Rows of Source/Target Array
vntD = Split(vntS(i, 1)) ' Split each row of Source Array.
j = UBound(vntD)
If j > cNum - 1 Then
j = cNum - 1
End If
If j <> -1 Then
For j = 0 To j
If j > 0 Then
vntT(i, 1) = vntT(i, 1) & " " & vntD(j)
Else
vntT(i, 1) = vntD(j)
End If
Next
End If
Next
' Copy Target Array into Target Range.
Range(Cells(cFirstR, cTarget), Cells(LastR, cTarget)) = vntT
End Sub