如何在Excel中的最后一项之前用“和”将单词列表连接成一个句子?

时间:2015-03-07 13:05:35

标签: excel join concatenation formula

我想在Excel中加入一个单词列表(不在VBA中......工作表中的Excel公式),符合以下规范:

公式应该忽略空单元格。

公式应该将单词与"和"连接起来。如果单元格数组中有多个项目,则在最终项目之前。

公式应该添加","项目之间如果有两个以上的项目。

实施例:     A1 =狗     A2 =猫     A3 =鸟     A4 =鱼     结果将是:狗,猫,鸟和鱼

A1=dog
A2=cat
A3=(empty cell)
A4=fish
Result would be: dog, cat, and fish

A1=dog
A2=(empty cell)
A3=bird
A4=(empty cell)
Result would be: dog and bird

A1=dog
A2=(empty cell)
A3=(empty cell)
A4=(empty cell)
Result would be: dog

非常好吗?我保证我已经搜索并搜索了答案。

编辑:谢谢ExcelArchitect,我明白了!这是我第一次使用自定义功能。您可以像工作表中的任何其他功能一样使用它!这太棒了。

不是为了推动我的运气,但如果结果中只有一个单词,如果有多个单词,我可以得到两个单元格与我的结果连接?示例:如果您为我创建的功能仅返回" dog",我希望它将单元格与文本(B1)连接起来"我最喜欢穿的是&# 34;然后"狗"然后另一个单元格(B2)表示" 。服装#&34;判刑#34;我最喜欢穿的是狗服装。"但是,如果它返回多个动物,它会连接另外两个像这样的细胞:Cell C1"我最喜欢的东西是"和#34;狗,猫和鸟"和Cell C2"服饰&#34。所以它会说"我喜欢穿的东西是狗,猫和鸟的服装。"

如果你很好奇,我的数据真的与动物或服装无关。我正在编写一个程序,将进行心理测试,然后根据测试分数创建一个解释性报告(我是心理学家)。

-Mary Anne

3 个答案:

答案 0 :(得分:2)

Mary Anne:

这是使用VBA的好时机!但如果你不想,有一种方法可以在没有它的情况下实现你的目标。

您必须在此处考虑所有可能的结果。 4种不同的动物意味着你有15种结果:

enter image description here

你的等式只需要考虑所有15.这是非常长的,因此得出结论。因此,如果您有超过4只动物想要变成短语,那么您应该去VBA路线。

这是我的设置:

enter image description here

A7中的公式如下:

=IF(AND(A2<>"", A3="", A4="", A5=""), A2, IF(AND(A2="", A3<>"", A4="", A5=""), A3, IF(AND(A2="", A3="", A4<>"", A5=""), A4, IF(AND(A2="", A3="", A4="", A5<>""), A5, IF(AND(A2<>"", A3<>"", A4="", A5=""), A2&" and "&A3, IF(AND(A2<>"", A3="", A4<>"", A5=""), A2&" and "&A4, IF(AND(A2<>"", A3="", A4="", A5<>""), A2&" and "&A5, IF(AND(A2="", A3<>"", A4<>"", A5=""),A3&" and "&A4, IF(AND(A2="", A3<>"", A4="", A5<>""), A3&" and "&A5, IF(AND(A2="", A3="", A4<>"", A5<>""),A4&" and "&A5, IF(AND(A2<>"", A3<>"", A4<>"", A5=""), A2&", "&A3&", and "&A4, IF(AND(A2<>"", A3<>"", A4="", A5<>""), A2&", "&A3&", and "&A5, IF(AND(A2<>"", A3="", A4<>"", A5<>""), A2&", "&A4&", and "&A5, IF(AND(A2="", A3<>"", A4<>"", A5<>""), A3&", "&A4&", and "&A5, A2&", "&A3&", "&A4&", and "&A5))))))))))))))

这是通过Excel:

enter image description here

玛丽安妮 - 我这样的书呆子,我不得不这样做。这是VBA解决方案,您可以拥有任意数量的名称!将此代码粘贴到工作簿中的新模块中(转到Developer - &gt; Visual Basic,然后插入 - &gt;新模块,然后粘贴),然后就可以像在常规函数中一样在工作表中使用它。只要给它一个名字的范围,你应该好好去! -Matt

Function CreatePhrase(NamesRng As Range) As String
'Creates a comma-separated phrase given a list of words or names
Dim Cell As Range
Dim l As Long
Dim cp As String

'Add commas between the values in the cells
For Each Cell In NamesRng
    If Not IsEmpty(Cell) And Not Cell.Value = "" And Not Cell.Value = " " Then
        cp = cp & Cell.Value & ", "
    End If
Next Cell

'Remove trailing comma and space
If Right(cp, 2) = ", " Then cp = Left(cp, Len(cp) - 2)

'If there is only one value (no commas) then quit here
If InStr(1, cp, ",", vbTextCompare) = 0 Then
    CreatePhrase = cp
    Exit Function
End If

'Add "and" to the end of the phrase
For l = 1 To Len(cp)
    If Mid(cp, Len(cp) - l + 1, 1) = "," Then
        cp = Left(cp, Len(cp) - l + 2) & "and" & Right(cp, l - 1)
        Exit For
    End If
Next l

'If there are only two words or names (only one comma) then remove the comma
If InStr(InStr(1, cp, ",", vbTextCompare) + 1, cp, ",", vbTextCompare) = 0 Then
    cp = Left(cp, InStr(1, cp, ",", vbTextCompare) - 1) & Right(cp, Len(cp) - InStr(1, cp, ",", vbTextCompare))
End If

CreatePhrase = cp
End Function

希望有所帮助! 马特,通过ExcelArchitect.com

答案 1 :(得分:0)

VBA更简单。公式非常复杂,因为Excel没有允许连接范围的本机函数。但是,鉴于您已经写过您最多可以拥有8只动物,可以根据您的规则使用以下公式连接A1:A8的内容。您可以在显而易见的位置更改公式中的这些位置。

我做了一个改变:我可能错了,但我相信英语规则表明应该省略前一个and之前的逗号,所以我这样做了。如有必要,可以添加它。 编辑:进一步调查显示美国和英国规则之间存在差异:美国规则是您所要求的,英国规则在结合前省略逗号。我将修改公式和UDF以符合美国惯例。

在公式中,修改是在comma之前放置and。 UDF的变化同样很小。

该公式由以下序列构成:

enter image description here

所以把这些公式放在一起,只是为了引用A1:A8,我们结束了这个怪物:

=SUBSTITUTE(IFERROR(SUBSTITUTE(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2),",",",and ",LEN(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2))-LEN(SUBSTITUTE(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2),",",""))),MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","),2,LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(CONCATENATE(",",A1,",",A2,",",A3,",",A4,",",A5,",",A6,",",A7,",",A8,","),",,",","),",,",","),",,",","))-2)),",",", ")

这是一个VBA解决方案,允许任意数量的项目;它按照与上述相同的规则连接。


Option Explicit
Function ConcatRangeWithAnd(RG As Range, Optional Delim As String = ", ")
    Dim COL As Collection
    Dim C As Range
    Dim S As String
    Dim I As Long

Set COL = New Collection
For Each C In RG
    If Len(C.Text) > 0 Then COL.Add C.Text
Next C

Select Case COL.Count
    Case 0
        Exit Function
    Case 1
        ConcatRangeWithAnd = COL(1)
    Case 2
        ConcatRangeWithAnd = COL(1) & " and " & COL(2)
    Case Else
        For I = 1 To COL.Count - 1
            S = S & COL(I) & ", "
        Next I
        ConcatRangeWithAnd = S & "and " & COL(COL.Count)
End Select

End Function

答案 2 :(得分:0)

使用新的TEXTJOIN函数,可以很容易地做到这一点。

步骤1:将TEXTJOIN函数与", "分隔符一起使用,并将ignore_empty设置为TRUE。这将使您用逗号分隔并连接字符串,而忽略空白值。

步骤2:使用COUNTA函数计算列表中非空白条目的数量。并减去1。此时,您可能希望使用MAX函数将值设置为1。

第3步:使用SUBSTITUTE函数用" and "替换在步骤2中计算出的最后一个逗号实例。

将它们放在一起:

=SUBSTITUTE(TEXTJOIN(", ",TRUE,A1:A14),", "," and ",MAX(1,COUNTA(A1:A14)-1))

插入所需的任何范围,而不是上面的公式中的A1:A14,您将得到一个逗号分隔,并在最后一个单词前加上和。