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.
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.
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
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)
)
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