Best way to sum numbers after specific text in a column

kcmuppet

Active Member
Joined
Nov 2, 2005
Messages
439
Office Version
  1. 365
Platform
  1. Windows
What would be the best way to sum the numbers in a column that follow specific text?
In the example below, I want to add the numbers following "K" and those following "GM"

Brooks_Sports_Global_Factory_List_Dec_2022.xlsx
AB
1K2
2GM3,K1
3K1, GM2
4
5Total K4
6Total GM5
Sheet2
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Can be achieved with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter",{{"Column1.2", Text.Trim, type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Trimmed Text", {}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Removed Columns", "Value", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Value.1", "Value.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Character Transition",{{"Value.2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Value.1"}, {{"Sum", each List.Sum([Value.2]), type nullable text}})
in
    #"Grouped Rows"
 
Upvote 1
Can be achieved with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter",{{"Column1.2", Text.Trim, type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Trimmed Text", {}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Removed Columns", "Value", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Value.1", "Value.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Character Transition",{{"Value.2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Value.1"}, {{"Sum", each List.Sum([Value.2]), type nullable text}})
in
    #"Grouped Rows"
Thanks Alan. That's a fascination solution, which I've saved for future reference, but given I'm sharing the sheet with others who can't run PQ I'm looking for a function solution
 
Upvote 0
Here's what I came up with.

Brooks_Sports_Global_Factory_List_Dec_2022.xlsx
ABC
1K2
2GM3,K1
3K1, GM2
4
5Total forK4
6Total forGM5
Sheet2
Cell Formulas
RangeFormula
C5C5=SUM(IFERROR(MID(C1:C3,SEARCH(B5,C1:C3)+LEN(B5),1)/1,0))
C6C6=SUM(IFERROR(MID(C1:C3,SEARCH(B6,C1:C3)+LEN(B6),1)/1,0))


Is there a better way?
 
Upvote 0
Does it need to work with 2010? Also, are they always 1 digit numbers as in your example?
 
Upvote 0
Depending on how much data you have, you could try:

Excel Formula:
=SUM(IFERROR(SUBSTITUTE(TEXTSPLIT(TEXTJOIN(",",TRUE,$C$1:$C$3),",",,TRUE),B5,"")+0,0))
 
Upvote 1
Solution
Depending on how much data you have, you could try:

Excel Formula:
=SUM(IFERROR(SUBSTITUTE(TEXTSPLIT(TEXTJOIN(",",TRUE,$C$1:$C$3),",",,TRUE),B5,"")+0,0))
Thanks, if i understand correctly, we join the text in the column adding commas, then split it at each comma, then substitute the text for those being counted with blank, and for every error (where it can't find the text for those being counted, we replace with 0, then sum what's left? Is that about right? Ingenious

I can't work out what the "+0" is for though? Is it to force the text digits to become a number (a bit like my "/1" effort above?
 
Upvote 0
I can't work out what the "+0" is for though? Is it to force the text digits to become a number (a bit like my "/1" effort above?
Yes. The IFERROR handles the situations where the value can't be coerced to a number.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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