JUAN MORENO
JUAN MORENO

Reputation: 13

How to get the first 3 words out of each cell of a range previously selected?

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

Answers (2)

Dominique
Dominique

Reputation: 17493

This is a proposal, using basic Excel formulas:

  • Replace the first space with an underscore
  • Replace the first space with an underscore (as a result, both first spaces are replaced by underscore)
  • Determine the location of the first space (which gives the location of the third space in the original text)
  • Take the text, at the left of the nth 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

VBasic2008
VBasic2008

Reputation: 54807

Split Names

Assumptions

There are two or three names per cell range (person):

  • First Name and Last Name or
  • First Name, Middle Name and Last Name.

You wanted the names from one column split into three columns.

The Code

  • Adjust the values in the constants section to fit your needs.
  • You can choose the same column letter or number if you want to overwrite the initial data, but do this after testing the code.

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

Second Version

  • Adjust the values in the constants section to fit your needs.
  • You can choose the same column letter or number if you want to overwrite the initial data, but do this after testing the code.

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

Related Questions