【问题标题】:Excel Concatenate Each Cell in RangeExcel连接范围内的每个单元格
【发布时间】:2016-06-06 15:04:58
【问题描述】:

我有两个公式可以找到我想要连接的范围。 一个公式返回 Range 开头的单元格地址,例如:

 '[AM Track.xlsm]Raw Data'!$A$10`  

另一个公式返回范围末尾的单元格地址,例如

 '[AM Track.xlsm]Raw Data'!$A$21`

如何连接此范围内的每个单元格?

【问题讨论】:

  • 我相信 Excel 2016 中有一个新功能可以做这样的事情。 @Jeeped 曾经向我提到过。但我找不到它/让它工作。也许他/她有办法解决你的问题。
  • @Ralph,我相信textjoin() 就是您所说的。 OP,如果你没有 Excel 2016,你需要一个 VBA 函数/UDF 来完成它。
  • 对 VBA 代码有什么建议吗?
  • 这应该让你开始创建一个循环:homeandlearn.org/excel_vba_for_loops.html

标签: excel excel-formula concatenation


【解决方案1】:

在 Excel 2016 中模拟 TextJoin 并满足您需求的快速 VBA 例程是:

Function textjoin(delimiter As String, ignoreEmpty As Boolean, startcell As Range, endcell As Range) As String
    Dim rngCell As Range

    For Each rngCell In Range(startcell, endcell).Cells
        If (ignoreEmpty And rngCell.Value <> "") Or Not ignoreEmpty Then
            If textjoin = "" Then textjoin = rngCell.Value Else textjoin = textjoin & delimiter & rngCell.Value
        End If
    Next rngCell
End Function

这与内置 Excel 2016 textjoin() 公式的区别在于最后两个参数是范围的开始和结束(而不是像 textjoin 想要的只是单个范围)。

要使用它,请转到您的 VBE (Alt+F11)。在工作簿的 VBAProject 中创建一个新模块。然后把这个粘贴进去。

【讨论】:

    【解决方案2】:

    感谢 Excel 2016 中关于 TextJoin 的评论,我发现:

    Option Explicit
    Function TEXTJOIN(delimiter As String, ignore_empty As String, ParamArray textn() As Variant) As String
        Dim i As Long
        For i = LBound(textn) To UBound(textn) - 1
            If Len(textn(i)) = 0 Then
                If Not ignore_empty = True Then
                    TEXTJOIN = TEXTJOIN & textn(i) & delimiter
                End If
            Else
                TEXTJOIN = TEXTJOIN & textn(i) & delimiter
            End If
        Next
        TEXTJOIN = TEXTJOIN & textn(UBound(textn))
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      相关资源
      最近更新 更多