Excel 2011 - 如何将 textJoin 的输出从单元格的值更改为单元格名称,以便我可以将其放入 Sum() 以添加这些值

Excel 2011 - How to change output of textJoin from value of cell, to the cell name, so I can put it in Sum() to add those values

我正在使用下面列出的 textJoin UDF 将来自不同行的值组合到一个单元格中;它显示每个值。但是,我想知道我是否可以操纵此 UDF 的输出,以便我可以添加值并获得值的总和,而不是简单地显示值。或者理想情况下,如果我可以只修改变量以指示它添加值。有谁知道是否可以指示此 UDF(我没有创建)输出单元格名称(A2、B2、C2),如果可以,我可以将该输出放在 Sum() 函数中,以便它添加 A2 +B2+C2?

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
        TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
    End Function

似乎应该有一种方法可以将单元格值的输出转换为单元格名称(即 C2、C3、C4 等),然后将整个内容放入 Sum() 函数中,这样它只是将单元格加在一起。或者,是否有一个函数可以广告放置在函数中的值而不是使用单元格名称?

我想添加突出显示的单元格 (G2)。我的值为 10 和 20。看来我应该能够使用 =Sum(textJoin(...)) 如果我可以获得 textJoin 来输出单元格名称(即 C2、C3)。

如果您希望它只对值求和,那么您可以在 UDF 的末尾添加类似这样的内容

    Dim total As Long
    Dim txtPart
    For Each txtPart In Split(TEXTJOIN, delim)
        total = total + CLng(txtPart)       
    Next txtPart
    TEXTJOIN = total

例子

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
Dim d As Long
Dim c As Long
Dim arr2()
Dim t As Long, y As Long
t = -1
y = -1
If TypeName(arr) = "Range" Then
    arr2 = arr.Value
Else
    arr2 = arr
End If
On Error Resume Next
t = UBound(arr2, 2)
y = UBound(arr2, 1)
On Error GoTo 0

If t >= 0 And y >= 0 Then
    For c = LBound(arr2, 1) To UBound(arr2, 1)
        For d = LBound(arr2, 1) To UBound(arr2, 2)
            If arr2(c, d) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
            End If
        Next d
    Next c
Else
    For c = LBound(arr2) To UBound(arr2)
        If arr2(c) <> "" Or Not skipblank Then
            TEXTJOIN = TEXTJOIN & arr2(c) & delim
        End If
    Next c
End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
    'add the below loop to add each number together
    Dim total As Long
    Dim txtPart
    For Each txtPart In Split(TEXTJOIN, delim)
        total = total + CLng(txtPart)

    Next txtPart
    TEXTJOIN = total
End Function