【问题标题】:Excel - Concatenate many columnsExcel - 连接多列
【发布时间】:2014-09-02 16:53:51
【问题描述】:

我正在尝试在 Excel 中连接一堆列。我知道我可以手动处理:

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

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

提前感谢您的帮助!

【问题讨论】:

标签: excel excel-formula vba


【解决方案1】:

作为用户函数采用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)。

这样使用:

【讨论】:

    【解决方案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 然后将包含您连接的字符串的内容。

    【讨论】:

      【解决方案3】:

      让我也发布我的功能。我过去也遇到过这个问题。
      当我尝试连接日期、错误和空白单元格时,通常会出现我的问题。
      因此,我尝试涵盖大多数使用以下内容的人:

      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。

      【讨论】:

        【解决方案4】:

        当连接范围单行或列时,您可以使用Application.Transpose 一次性完成此操作,以避免范围循环

        这个 UDF 有三个参数

        1. 一维范围(可以是列或行)
        2. 可选的分隔符(如果没有条目,则使用,
        3. 一个可选条目,用于指定范围是否为一行(输入 TRUE 作为范围 - 进一步认为我将更新 UDF 以自动检测范围是 row 还是 column 基于)李>

        请注意,就其他答案而言

        • IIF 评估 TRUEFALSE 参数,因为 VBA 没有 [短路]( http://en.wikipedia.org/wiki/Short-circuit_evaluation)。所以IFF 在循环内部可能很昂贵
        • 在将长字符串连接到短字符串的组合输出时,而不是长与短,然后再次长与短

        代码

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

        【讨论】:

          【解决方案5】:

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

          Sub ConcatColumns()

          Do While ActiveCell &lt;&gt; "" '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

          【讨论】:

          • 这并没有解决在单元格值之间放置逗号的需要。
          • 它只是在向下移动一行之前连接最后两个单元格。
          猜你喜欢
          • 2020-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-09
          相关资源
          最近更新 更多