1

我正在尝试使用 Excel 中的公式来用逗号分隔单元格中的一堆单词。如果单元格中的单词超过 5 个,我只想获取前 5 个单词。要获取单元格中的前五个单词并用逗号分隔它们,我使用以下命令:

=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",5))-1), " ", ", ")

这工作正常。但是这个问题,因为这里的数字 5,如果我一个单元格包含少于 5 个单词,我会得到一个错误。我试图用这个代替5:

LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1

所以我的功能变成了这样:

=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1))-1), " ", ", ")

但这不起作用,它给了我一个错误。知道我该怎么做吗?

如果它的第一个字符是“-”(不带引号),我也想忽略第一个单词并从第二个单词开始。所以换句话说,我想要这样的东西:

我非常爱我的生活应该回报我,爱我的生活,非常
-我非常爱我的生活应该回报我,爱我的生活,非常(“-”被忽略”)
我爱我的生活应该回报我,爱,我的

提前感谢您的帮助

4

5 回答 5

2

这是一种有些不同的方法。除了“小于5”的问题外,还处理了“5字结尾没有空格”的问题:

=LEFT(A1,FIND("^",SUBSTITUTE(A1 & "^"," ","^",5))-1)

编辑1:我刚刚注意到关于前导“-”的部分。我的添加不是很优雅,但它处理它,以及TRIMS任何尾随空格:

=TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))

编辑 2:哦,是的,逗号:

=SUBSTITUTE(TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))," ",",") 

在此处输入图像描述

于 2013-08-08T02:33:40.293 回答
2

尝试这个:

=TRIM(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-"," "))," ","),",",REPT(" ",99),5),99))

于 2013-08-09T12:54:44.690 回答
1

即使破折号后没有空格或文本中有多余的空格,这也会起作用。我经常发现输入不是很干净

=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5),IFERROR(FIND("*",SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5))-1,999))," ",",")

可能的空格或缺失空格的示例

编辑:在对 István 发表评论后,我也让我的作品完美无瑕。

=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-","  ",1)
&MID(TRIM(A1),2,999))," ","*",5),IFERROR(FIND("*",SUBSTITUTE(
TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-","",1)&MID(TRIM(A1),2,999))," ","*",5))-1,999))," ",",")

现在全部修复

但我认为他的更优雅。

于 2013-08-08T04:16:10.793 回答
0

我想我也会把我的帽子扔进擂台。我认为这个公式应该涵盖以下基础:

=SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1&" ","- ",""))," ",REPT(" ",99)),99*5))," ",",")
于 2013-08-10T06:29:28.203 回答
0

尝试这个:

=SUBSTITUTE(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)),FIND("|",SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)))-1),",,",",")

该公式通过以下步骤起作用:

  • 删除任何前导的破折号空间
  • 修剪任何前导或尾随空格
  • 插入逗号空格代替空格并添加尾随逗号空格
  • 计算 5 和字符串中的单词数中的较小者
  • 输入“|” 如果字符串少于五个单词,则代替第五个逗号空格或尾随逗号空格
  • 确定“|”的位置
  • 去掉“|” 以及它右边的所有字符
  • 删除由于初始字符串中的任何单个嵌入逗号而导致的任何双引号

如果您愿意考虑 VBA 解决方案,可以将这个复杂的表达式替换为用户定义的函数:

Function words5(InputString As String) As String
    Dim wordArray As Variant
    wordArray = Split(Trim(Replace(InputString, _        'remove "-", put words into array
        "-", "", , 1)), " ")
    ReDim Preserve wordArray(LBound(wordArray) To _      'drop all but the first 5 words
        WorksheetFunction.Min(UBound(wordArray), 5 - 1))
    words5 = Replace(Join(wordArray, ", "), ",,", ",")   'rejoin the words with ", "
End Function                                             'separator

与工作表公式相比,使用此代码的好处是它的可维护性,如果不访问组合成单个表达式的原始构建块,就无法理解或安全地更改它。

该代码必须安装在使用它的工作簿中,或者安装在标准 Personal.xlsb 工作簿或插件工作簿中。

要使用该功能,请将其复制并粘贴到标准模块中,该模块可以通过 VBA 编辑器插入到工作簿中。您可以使用Visual Basic功能区的“开发人员”选项卡上的按钮打开编辑器。

于 2013-08-08T03:19:21.397 回答