使用逗号连接如果不是空列表

时间:2017-07-21 15:21:26

标签: excel excel-formula excel-2013

在Excel中,我需要将每个其他单元格连接成一个" master"细胞。我在=SUBSTITUTE(TRIM(G2 & " " & BC2 & " " & BE2 & " " & BG2), " ", ", ")之前使用过这个公式来征服,但是当我连接的数据中有逗号时,这会改变数据。

我需要连接的细胞范围是从G2一直到BG2的所有其他细胞。在处理涉及逗号列表的连接时,最好的做法是什么?

修改
根据我的意思改变数据就是这个

S223 - Pills, S2323 - Patterns - Backstock, 1/Var

使用上面的公式

S223, -, Pills,, S2323, -, Patterns, -, Backstock,, 1/Var

3 个答案:

答案 0 :(得分:1)

VLOOKUP with multiple criteria returning values in one cell

上使用UDF

公式:

 =TEXTJOIN(", ",TRUE,IF(MOD(COLUMN(G2:BG2),2)=1,G2:BG2,""))

作为数组公式,需要使用Ctrl-Shift-Enter确认。

答案 1 :(得分:0)

很久以前,我发现这个代码是在线的,因为我需要它来处理我的个人项目。这样做是需要一个字符串并用指定的字符替换每个“不允许的”值。在你的情况下,我会想象你允许除“,”之外的每个字符,并用“”或“”替换它。这样,A, - ,B, - ,C,将成为A - B - C

Function cleanString(text As String) As String

    Dim output As String
    Dim c
    For i = 1 To Len(text)
        c = Mid(text, i, 1)
        If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z" Or c = " " Or c = "-" Or c = "é" Or c = "É" Or c = "_") Then ' <=list of allowed values in a string
            output = output & c
        Else
            output = output & " " '<= what unallowed values gets replaced by
        End If
    Next
    cleanString = output
End Function

希望这可以提供帮助,我在您的问题中添加标签时考虑了VBA。

答案 2 :(得分:0)

来自answers.microsoft.com的以下UDF应该是您之后的

Function TEXTJOIN(delimiter As String, ignore_empty As String, ParamArray textn() As Variant) As String
    Dim i As Long
    For i = LBound(textn) To UBound(textn) - 1
        If Len(textn(i)) = 0 Then
            If Not ignore_empty = True Then
                TEXTJOIN = TEXTJOIN & textn(i) & delimiter
            End If
        Else
            TEXTJOIN = TEXTJOIN & textn(i) & delimiter
        End If
    Next
    TEXTJOIN = TEXTJOIN & textn(UBound(textn))
End Function