Reputation: 13
What I exactly need is to get the first 3 words out of each cell of the range selected, and then set it in the same place (each cell), so that I end up with the first 3 words in each cell. It doesn´t matter the number of words there were before. Basically, I need a code with a bucle to do that with each cell in the selection.
I´ve tried to use formula local, but it doesn´t work.
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
What I expect is to get the first 3 words in each cell of the column (range previously selected).
Upvotes: 1
Views: 212
Reputation: 17493
This is a proposal, using basic Excel formulas:
n
th character.Hereby the formulas (the original text is in cell B2
):
B3 : =SUBSTITUTE(B2;" ";"_";1)
B4 : =SUBSTITUTE(B3;" ";"_";1)
B5 : =FIND(" ";B4)
B6 : =LEFT(B2;B5-1)
Upvotes: 0
Reputation: 54807
There are two or three names per cell range (person):
You wanted the names from one column split into three columns.
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
Upvotes: 1