VBA Function Split Sum School

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking to modify my code below. I need to update my function to be able to lookup a school and get the sum. For instance, Jefferson and get 167 or Washington and get 241. Thanks in advance.

VBA Sum Parenthesis Range.xlsm
A
1Jefferson(100), Madison(25)
2Washington(230), Jackson(23), Lincoln(243)
3Beardsley(35), Jefferson(24)
4Cleveland(223)
5Lincoln(9), Washington(11), Jefferson(43)
Sheet2


VBA Code:
Function SumP(r As Range) As Variant
Dim i As Long
Dim Parts() As String
For Each xcell In r
Parts = Split(Replace(xcell, ")", "("), "(")
For i = 1 To UBound(Parts)
SumP = SumP + Val(Parts(i))
Next
Next
End Function
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
VBA Code:
Function SumP(r As Range) As Variant
Application.Volatile
Dim i As Long, xCell As Range
Dim Parts() As String
For Each xCell In r
Parts = Split(Replace(xCell, ")", "("), "(")
For i = 1 To UBound(Parts)
SumP = SumP + Val(Parts(i))
Next
Next
End Function
 
Upvote 0
VBA Code:
Function SumP(r As Range) As Variant
Application.Volatile
Dim i As Long, xCell As Range
Dim Parts() As String
For Each xCell In r
Parts = Split(Replace(xCell, ")", "("), "(")
For i = 1 To UBound(Parts)
SumP = SumP + Val(Parts(i))
Next
Next
End Function
Thanks for the response but I need another parameter in the function like

Function SumP(r As Range, Sch as string) As Variant

so that I could sum the values of the school that I select.
 
Upvote 0
Try again
VBA Code:
Function SumP(r As Range, Sch As String) As Long
Application.Volatile
Dim i As Long, xCell As Range
Dim Parts() As String
For Each xCell In r
    Parts = Split(Replace(Replace(xCell, ",", ""), ")", "("), "(")
    For i = LBound(Parts) To UBound(Parts) Step 2
        If Trim(Parts(i)) = Sch Then SumP = SumP + Val(Parts(i + 1))
    Next i
Next
End Function
 
Last edited:
Upvote 0
Here is more compact, single-loop function that you can also consider...
VBA Code:
Function SumP(Rng As Range, Sch As String) As Long
  Dim V As Variant
  For Each V In Split(LCase(Join(Application.Transpose(Rng))), Sch & "(")
    SumP = SumP + Val(V)
  Next
End Function
 
Last edited:
Upvote 0
Do you need to use vba or could you use standard worksheet functions?
Depending on your Excel version** you may need to confirm the formula with Ctrl+Shift+Enter, not just Enter.

** I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Stephen_IV.xlsm
ABCD
1Jefferson(100), Madison(25)Jefferson167
2Washington(230), Jackson(23), Lincoln(243)Washington241
3Beardsley(35), Jefferson(24)
4Cleveland(223)
5Lincoln(9), Washington(11), Jefferson(43)
Sheet1
Cell Formulas
RangeFormula
D1:D2D1=SUM(--LEFT(SUBSTITUTE(REPLACE(", "&A$1:A$5&", "&C1&"(0",1,SEARCH(", "&C1&"(",", "&A$1:A$5&", "&C1&"(")+LEN(C1)+2,0),")",REPT(" ",20)),20))
 
Upvote 0
Thanks to everyone who applied to this post! I have learned a ton!
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top