如何从先前选择的范围的每个单元格中获得前三个单词?

时间:2019-01-20 08:26:36

标签: excel vba excel-2010

我真正需要的是从所选范围的每个单元格中获取前三个单词,然后将其设置在相同的位置(每个单元格),这样我最终得到每个单元格中的前三个单词。以前的单词数量无关紧要。基本上,我需要一个带有气泡的代码才能对所选内容中的每个单元格进行处理。

我尝试使用局部公式,但这不起作用。

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个字(先前选择的范围)。

2 个答案:

答案 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)