我真正需要的是从所选范围的每个单元格中获取前三个单词,然后将其设置在相同的位置(每个单元格),这样我最终得到每个单元格中的前三个单词。以前的单词数量无关紧要。基本上,我需要一个带有气泡的代码才能对所选内容中的每个单元格进行处理。
我尝试使用局部公式,但这不起作用。
Sub EXTRAER_NOMBRES_Y_APELLIDO()
'Convierte los textos seleccionados a formato de nombre propio
'La primera letra en mayúscula y el resto en minúsculas
'Dim CELDA As String
'Dim B As Integer
For Each CELDA In Selection
'CELDA.Value = Left(Range("Y3"), 5)
'Range("Y3") = Left(Range("Y3"), 5)
'CELDA.Value = Left(CELDA, 3)
ActiveCell.FormulaLocal = "=LEFT(Planilla[@Estudiante];FIND(" ";Planilla[@Estudiante])-1)"
Next CELDA
End Sub
我期望的是在该列的每个单元格中获得前3个字(先前选择的范围)。
答案 0 :(得分:1)
每个单元格区域(人)有两个或三个名称:
您希望将一列中的名称分成三列。
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
答案 1 :(得分:0)
这是一个建议,使用基本的Excel公式:
n
个字符左侧的文本。这里是公式(原始文本在单元格B2
中):
B3 : =SUBSTITUTE(B2;" ";"_";1)
B4 : =SUBSTITUTE(B3;" ";"_";1)
B5 : =FIND(" ";B4)
B6 : =LEFT(B2;B5-1)