在系列中的每个单元格值之间追加字符串

时间:2013-07-18 18:51:12

标签: excel excel-vba excel-formula concatenation worksheet-function vba

假设我有一系列像这样的细胞:

    A
1  Foo
2  Bar
3  Hello
4  World
5  Random Text

我想做的是让我的公式的结果填充另一个单元格:

Foo, Bar, Hello, World, Random Text

现在,我知道如何将两个单元连接起来:

=A1&", "&A2

但是我怎么能对整个系列做同样的事情呢?

3 个答案:

答案 0 :(得分:1)

在单元格B1中放置逗号和空格,然后使用以下公式:

=CONCATENATE(A1,B1,A2,B1,A3,B1,A4, B1, A5)

您可以尝试以下问题的几个答案,包括VBA选项和公式:

Need to concatenate varying number of cells...

答案 1 :(得分:1)

在B1中使用=A1,然后在B2中=B1&", "&A2并向下复制似乎可行。

答案 2 :(得分:1)

这是您可以使用的功能。只需将其放在工作簿代码模块中,然后就可以在以下单元格中输入:

=JoinRange(A1:A6)=JoinRange(A2:D15)

Public Function JoinRange(ByVal rng As Range) As String
    Dim dlmt As String: dlmt = ","
    Dim multiRow As Boolean: multiRow = rng.Rows.Count > 1
    Dim r As Long, c As Long

    Select Case rng.Columns.Count
        Case 1
            If multiRow Then
                JoinRange = Join(Application.WorksheetFunction.Transpose(rng), dlmt)
            Else:
                'a single cell
                JoinRange = rng
            End If
        Case Is > 1
            If multiRow Then
                'a 2d range of cells:
                For r = 1 To rng.Rows.Count
                    For c = 1 To rng.Columns.Count
                        JoinRange = JoinRange & rng(r, c) & dlmt
                    Next
                Next
                JoinRange = Left(JoinRange, Len(JoinRange) - 1)

            Else:
                JoinRange = Join(Application.WorksheetFunction.Transpose( _
                                Application.WorksheetFunction.Transpose(rng)), dlmt)
            End If
        Case Else

     End Select

End Function