将一个常量值前置到逗号分隔列表的所有项目

时间:2014-08-11 08:18:46

标签: excel excel-vba excel-formula excel-2007 excel-2010 vba

我有两列。我想连接ColumnA和ColumnB中的单元格,以便给出ColumnD中说明的输出。我尝试了ColumnA&ColumnB但是C列中显示的输出与所需的输出不对应。我该如何解决这个问题?

Input1 |Input2  |Output   |Desired output
ColumnA|ColumnB |ColumnC  |ColumnD 
-----------------------------------------
A      |1,2,3,4 |A1,2,3,4 |A1, A2, A3, A4
B      |5,3,6,7 |B5,3,6,7 |B5, B3, B6, B7 
C      |8,9     |C8,9     |C8, C9
D      |10,11,45|D10,11,45|D10, D11, D45

2 个答案:

答案 0 :(得分:2)

你可以试试这样的事情

=A1 & SUBSTITUTE(B1;",";", " & A1)

答案 1 :(得分:0)

这个用户定义的功能对我有用:

Function prependToAllElements(prefix As String, commaSeparatedList As String) As String
    Dim i As Long
    Dim s() As String
    s = Split(commaSeparatedList, ",")
    For i = LBound(s) To UBound(s)
        s(i) = prefix & s(i)
    Next i
    prependToAllElements = Join(s, ",")
End Function

单元格D1中的示例用法:

=prependToAllElements(A1,B1)