0

Image

Hi

This is a leave roster updated in this format. I need to take the sum of the numbers (inside brackets).

The list goes on i can't change the format now, appreciate if anyone can help with calculating the sum using a formula.

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
kevin_s_p
  • 17
  • 5

3 Answers3

1

If you have access to TEXTJOIN function, you can use following array formula for any count of parentheses:

=SUM(IFERROR(FILTERXML("<a><b>" & SUBSTITUTE(SUBSTITUTE(TEXTJOIN("",TRUE,A2:L3),"(","#</b><b>"),")","</b><b>") & "</b></a>","//b"),0))

Array formula after editing is confirmed by pressing ctrl + shift + enter

enter image description here

basic
  • 11,673
  • 2
  • 9
  • 26
  • Thought about this too, but i don't have access to `TEXTJOIN`, so substitute ist limited to one cell. What ist the meaning of `//b`, as i would have [used](https://stackoverflow.com/questions/61837696/excel-extract-substrings-from-string-using-filterxml) `//s[position() mod 2 = 1]`, meaning every odd element? – OverflowStacker Oct 14 '20 at 13:13
  • 1
    @OverflowStacker ```//b``` returns all elements as an array. In my formula, anything that is outside the parentheses is converted to text by adding a ```#``` symbol, so only the numbers in parentheses are summed. – basic Oct 14 '20 at 15:00
0

A solution without helper columns and a maximum of 2 pairs of parentheses would be this ARRAY FORMULA: CTRL + SHIFT + ENTER

=SUM(
    VALUE(MID(A1:A2,SEARCH("(",A1:A2)+1,SEARCH(")",A1:A2)-SEARCH("(",A1:A2)-1)),
    IFERROR(VALUE(MID(A1:A2,SEARCH("(",A1:A2,SEARCH("(",A1:A2)+1)+1,SEARCH(")",A1:A2,SEARCH(")",A1:A2))-SEARCH("(",A1:A2)-1)),0)
    )

Adapt the ranges to your needs.
enter image description here

OverflowStacker
  • 1,340
  • 1
  • 10
  • 17
0

If you can use vba, try using the user-defined function below.

Function mySum(rngDB As Range)
    Dim mCol As Object 'MatchCollection
    Dim Ws As Worksheet
    Dim rng As Range
    Dim strPattern As String
    Dim s As String
    Dim i As Integer, n As Integer
    Dim vSum() As Variant
    
    Application.Volatile
    Set Ws = ActiveSheet
     strPattern = "(()([0-9]{1,})())"
    For Each rng In rngDB
        s = rng.Value
        Set mCol = GetRegEx(s, strPattern)
        If Not mCol Is Nothing Then
            For i = 0 To mCol.Count - 1
                n = n + 1
                ReDim Preserve vSum(1 To n)
                vSum(n) = Val(mCol.Item(i))
            Next i
        End If
    Next
    If n Then
        mySum = WorksheetFunction.Sum(vSum)
    End If

End Function

Function GetRegEx(StrInput As String, strPattern As String) As Object
    Dim RegEx As Object 'New RegExp
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Global = True
        .IgnoreCase = False
        .MultiLine = True
        .Pattern = strPattern
    End With
    If RegEx.test(StrInput) Then
        Set GetRegEx = RegEx.Execute(StrInput)
    End If
End Function

image

enter image description here

Dy.Lee
  • 7,527
  • 1
  • 12
  • 14