Excel – 连接许多列

我试图连接Excel中的一堆列。 我知道我可以手动做:

=A1&", "&B1&", "&C1 (依此类推)

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

提前感谢任何帮助!

作为使用range的用户function

 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 

如果需要更改Varianttypes( string ,很有可能)。

像这样使用:

在这里输入图像说明

我会用这个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将有你的连接string的内容。

让我发布我的function。 我也遇到过这个问题。
当我尝试连接date,错误和空白单元格时,通常会出现我的问题。
所以我试图覆盖下面的大部分使用:

 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 

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

当连接单个行或一列的范围时,您可以使用Application.Transpose执行此操作以避免范围循环

这个UDF有三个参数

  1. 一维范围(可以是列或行)
  2. 一个可选的分隔符(如果没有entrey则使用)
  3. 一个可选的条目来指定范围是否是一个行(对于一个范围inputTRUE – 进一步认为我将更新UDF来自动检测范围是row column BASED)

请注意,在其他答案方面

 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,", ")

在这里输入图像说明

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

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