0

我正在使用下面列出的 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。如果我可以让 textJoin 输出单元格名称(即 C2、C3),我似乎应该能够使用 =Sum(textJoin(...)) 。

在此处输入图像描述

4

1 回答 1

1

如果您希望它只是总计值,那么您可以在 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
于 2017-07-06T20:20:08.307 回答