2014-09-02 32 views
4

我想连接Excel中的一堆列。我知道我可以手动做:Excel - 连接许多列

=A1&", "&B1&", "&C1(等)

,但我有40列,我正在寻找一种方法来简化这一过程。

在此先感谢您的帮助!

+1

http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/ – 2014-09-02 16:56:58

回答

3

作为用户函数采取range

Public Function ClarkeyCat(ByRef rng As Range) As Variant 

Dim c As Range 
Dim ans As Variant 

For Each c In rng 

If (c.Value <> "") Then 
    ans = IIf(ans = "", "", ans & ",") & c.Value 
End If 

Next 

ClarkeyCat = ans 
End Function 

改变Variant类型,如果你需要(以string,最有可能的)。

使用这样的:

enter image description here

+1

这可能是最优雅的解决方案。 – 2014-09-02 17:30:49

0

您可以随时使用Visual Basic For Applications(VBA)。它是Office的微软语言。以下是您可能要查找的示例,但请尝试使用Google Machine了解有关VBA的更多信息以及如何将此代码输入到电子表格中。

Sub ConcatColumns()

Do While ActiveCell <> "" 'Loops until the active cell is blank.

'The "&" must have a space on both sides or it will be 
    'treated as a variable type of long integer. 

    ActiveCell.Offset(0, 1).FormulaR1C1 = _ 
    ActiveCell.Offset(0, -1) & " " & ActiveCell.Offset(0, 0) 

    ActiveCell.Offset(1, 0).Select 

Loop

End Sub

+1

这并没有解决逗号需要放置在单元格值之间。 – 2014-09-02 17:37:22

+1

它只是将一行中的最后两个单元格连接在一行之前。 – 2014-09-02 17:43:02

2

我会用VBA这一点。对于每一列你会想要类似的东西(假设值在第1行)

myString = "" 
for i = 1 to 40 
    if i <> 40 then 
    myString = myString & Cells(1, i) & ", " 
    else: 
    myString = myString & Cells(1, i) 
    end if 
next i 

myString将会有你的连接字符串的内容。

1

当串联一系列单个行或列,你可以使用Application.Transpose避免范围单杆做循环

此UDF有三个参数

  1. 1D范围(可以是列或行)
  2. A n可选分隔符(如果没有entrey,则使用,
  3. 可选项用于指定范围是否为一行(对于范围,请输入TRUE - 进一步认为我将更新UDF以自动检测范围是row OR column为主)

注意的是,在其他的答案

代码

Function ConCat(rng1 As Range, Optional StrDelim As String, Optional bRow As Boolean) As String 
Dim x 
If StrDelim = vbNullString Then StrDelim = "," 
x = Application.Transpose(rng1) 
If bRow Then x = Application.Transpose(x) 
ConCat = Join(x, StrDelim) 
End Function 

在下面

  • 式(D1)的例子是=concat(A1:C1,",",TRUE)
  • E1公式是=concat(E3:E5,", ")

enter image description here

2

让我发表我的功能了。我也遇到过这个问题。
当我尝试连接日期,错误和空白单元格时,通常会出现我的问题。
所以我尝试使用下面覆盖的大多数人:

Function CONCATPLUS(ref_value As Range, Optional delimiter As Variant) As String 
    Dim cel As Range 
    Dim refFormat As String, myvalue As String 

    If ref_value.Cells.Count = 1 Then CONCATPLUS = CVErr(xlErrNA): Exit Function 
    If IsMissing(delimiter) Then delimiter = " " 

    For Each cel In ref_value 
     refFormat = cel.NumberFormat 
     Select Case TypeName(cel.Value) 
     Case "Empty": myvalue = vbNullString 
     Case "Date": myvalue = Format(cel, refFormat) 
     Case "Double" 
      Select Case True 
      Case refFormat = "General": myvalue = cel 
      Case InStr(refFormat, "?/?") > 0: myvalue = cel.Text 
      Case Else: myvalue = Format(cel, refFormat) 
      End Select 
     Case "Error" 
      Select Case True 
      Case cel = CVErr(xlErrDiv0): myvalue = "#DIV/0!" 
      Case cel = CVErr(xlErrNA): myvalue = "#N/A" 
      Case cel = CVErr(xlErrName): myvalue = "#NAME?" 
      Case cel = CVErr(xlErrNull): myvalue = "#NULL!" 
      Case cel = CVErr(xlErrNum): myvalue = "#NUM!" 
      Case cel = CVErr(xlErrRef): myvalue = "#REF!" 
      Case cel = CVErr(xlErrValue): myvalue = "#VALUE!" 
      Case Else: myvalue = "#Error" 
      End Select 
     Case "Currency": myvalue = cel.Text 
     Case Else: myvalue = cel 
     End Select 

     If Len(myvalue) <> 0 Then 
      If CONCATPLUS = "" Then 
       CONCATPLUS = myvalue 
      Else 
       CONCATPLUS = CONCATPLUS & delimiter & myvalue 
      End If 
     End If 
    Next 
End Function 

截至目前,我还没有遇到一个单元格输入这个功能不能连接。
随意适应您的需求或心中的内容。 HTH。