获得句子中每个单词的第一个字母,但某些关键字

时间:2017-02-05 09:39:26

标签: excel excel-formula

我认为这个很棘手,但我想知道是否有人可能有解决方案。

我需要创建一个公式来获取名称列表中每个单词中的第一个字母,例如:

  

中国电子科技大学

电子科技大学('',''','和','a'如果可能的话,应该避免)

到目前为止我尝试了什么:

=(LEFT(H2)&MID(H2,FIND("#",SUBSTITUTE(H2&" "," ","#",1))+1,1)&MID(H2,FIND("#",SUBSTITUTE(H2&" "," ","#",2))+1,1))&J2&M2

4 个答案:

答案 0 :(得分:2)

好问题!我认为您最好的选择是使用VBA创建自己的功能。将下面的代码添加到VBA编辑器中的新模块中,您可以在工作表上使用此功能,如下所示:

=FirstLetters(A1)

根据您的示例,

会返回UESTC
Function FirstLetters(str As String)
    Dim words As Variant
    Dim resultStr As String
    Dim i As Integer

    words = Split(str)
    For i = 0 To UBound(words)
        Select Case words(i)
            Case "the", "of", "and", "a"
                'ignore
            Case Else
                resultStr = resultStr & Left(words(i), 1)
        End Select
    Next i

    FirstLetters = UCase(resultStr)
End Function

答案 1 :(得分:2)

好吧,我发现了一个有点荒谬的答案,它基本上连接了很多ifs来遍历每个单词(VBA在这里会更容易)。以下公式最多9个字。如果您的字符串数量更多,则只需添加一些FIND(A1," ")+1。如果公式不适合您,请删除其中的返回值。

=IFERROR(UPPER(
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=1,IFERROR(IF(SUM(--(LEFT(A1,FIND(" ",A1)-1)={"the","of","a","and"})),"",LEFT(A1,1)),LEFT(A1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=2,IF(SUM(--(MID(A1,FIND(" ",A1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1)+1),LEN(A1))-(FIND(" ",A1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=3,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=4,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=5,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=6,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=7,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=8,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1,1)),"")&
IF(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1>=9,IF(SUM(--(MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1)+1,IFERROR(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1)+1),LEN(A1))-(FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1)+1))={"the","of","a","and"})),"",MID(A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1,FIND(" ",A1)+1)+1)+1)+1)+1)+1)+1)+1,1)),"")
),"")

答案 2 :(得分:0)

第1步:

使用替换删除您要丢弃的内容。

语法:

SUBSTITUTE(text, old_text, new_text, [instance_num])

步骤2:

在任何单元格中保持值1(假设为B1)。

第3步:

使用以下公式获得第一个字母。

=IF(LEN(A1)>=B1,RIGHT(LEFT(A1,B1),1),"  ")

A1是您的输入,B1是值1.如果您想要第一个字母。如果你想要第二个字母B1值应该是2 ..依此类推。

如果你想获得第一个字:

=LEFT(A1,SEARCH(" ",A1)-1)

答案 3 :(得分:0)

如果您正在使用具有CONCAT功能的Excel 2016或365,则以下公式适用于任意长度的字符串:

=LEFT(A1,1) & CONCAT(IF(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," the "," "), " of "," "), " a "," "), " and ", " "),ROW(OFFSET($A$1,0,0,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," the "," "), " of "," "), " a "," "), " and ", " ")),1)),1)=" ",MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," the "," "), " of "," "), " a "," "), " and ", " "),ROW(OFFSET($A$1,1,0,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," the "," "), " of "," "), " a "," "), " and ", " ")),1)),1),""))

请注意,这是一个数组公式,必须通过双击单元格,粘贴公式,然后按CTRL+SHIFT+ENTER

输入

它将始终包含第一个单词的第一个字母,即使它是要排除的关键词之一。排除所有其他关键字。一般来说,第一个单词的第一个字母应该总是包含在内,但是如果你也需要删除它,我可以进一步修改公式。