【发布时间】:2017-07-12 22:05:56
【问题描述】:
我不明白为什么这个 UDF 没有一致地添加数字。出于说明目的,我对输出进行了颜色编码。它准确地添加了所有内容,但由于某种原因,它不想添加应该等于 64 的 4 个数字,而是输出 46.5,但没有任何数字组合得出 46.5。
这是我正在使用的 UDF。
Function TEXTJOINSUM(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
TEXTJOINSUM = TEXTJOINSUM & 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
TEXTJOINSUM = TEXTJOINSUM & arr2(c) & delim
End If
Next c
End If
TEXTJOINSUM = Left(TEXTJOINSUM, Len(TEXTJOINSUM) - Len(delim))
'add the below loop to add each number together
Dim total As Double
Dim txtPart
For Each txtPart In Split(TEXTJOINSUM, delim)
total = total + CDbl(txtPart)
Next txtPart
TEXTJOINSUM = total
End Function
【问题讨论】:
-
列 C 和 G 设置为“数字”,如果这有影响的话。
-
查看您的数据。 A14 中的日期不是真正的日期,或者与它相关联的时间,或者 B14 有空格或其他不可见的字符。该代码对我有用,尽管 Excel 将使用本机公式做很多工作。错误在于数据 A14 或 B14。重新输入这两个单元格,我敢打赌它会起作用。
-
“没有任何数字的组合可以得出 46.5” - C11+C12+C13 怎么样?
-
@YowE3K 哎呀,好收获。
-
@ScottCraner 谢谢!如果这是答案形式,我会接受你的答案。就这么简单。我在第 14 行重新输入了数据,它更正了问题。
标签: excel user-defined-functions excel-2011 vba