0

I can't figure out why this UDF is not adding numbers consistently. I color coded the output for illustrative purposes. It adds everything accurately, but for some reason it doesn't want to add the 4 numbers which should equal 64, but instead it outputs 46.5, but there is no combination of any numbers that comes out to 46.5.

enter image description here

This is the UDF I'm using.

    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
Community
  • 1
  • 1
Devil07
  • 141
  • 1
  • 8
  • Columns C and G are set to "Number", if that makes a difference. – Devil07 Jul 12 '17 at 22:08
  • 2
    Look at your data. Either the date in A14 is not a true date or there is time associated with it, or B14 has spaces or other non visible characters. The code works for me, albeit it is a lot of work for something Excel will do with native formulas. The error lays in the data either A14 or B14. Retype those two cells and I bet it will work. – Scott Craner Jul 12 '17 at 22:26
  • 1
    "there is no combination of any numbers that comes out to 46.5" - how about C11+C12+C13? – YowE3K Jul 12 '17 at 22:28
  • @YowE3K oops, good catch. – Devil07 Jul 13 '17 at 00:26
  • @ScottCraner thank you!, if this was in answer form I would have accepted your answer. It was as simple as that. I re-entered data in line 14 and it corrected the issue. – Devil07 Jul 13 '17 at 13:39

1 Answers1

2

No need for the complicated UDF, use SUMIFS():

=SUMIFS(C:C,A:A,E2,B:B,F2)

enter image description here

Scott Craner
  • 148,073
  • 10
  • 49
  • 81
  • I didn't realize that my version of Excel had this SUMIFS, while it doest exactly answer my question about the error with the UDF, you answered it in the comments section to my question. You should get 2X points. This is a much better way to accomplish what I was doing, the reason I used the UDF is that I was already using it for textJoin (since my version of excel) did not have the textJoin functionality, so I just added the adding part to the end. – Devil07 Jul 13 '17 at 13:44
  • For this issue I would not use the UDF, and is why I put the "answer" in the comments and the best method here. I wanted to give you the answer but if other users find this, I want them to use the formula. I wish I had been around on your earlier post regarding this, I would have steered you to this method days ago. – Scott Craner Jul 13 '17 at 13:48
  • It would have saved me several hours of head aches, but I got some good experience out of it. This is a much more elegant solution. – Devil07 Jul 13 '17 at 13:50