跳到主要内容

如何仅在Excel中将括号中的数字求和?

如果有文本字符串列表,而您只想将括号中的所有数字加起来,如下图所示,您如何快速处理它? 在本文中,我为您介绍了一些技巧,使您仅在Excel中将括号中的数字求和。


方括号内的数字总和仅包含公式

要仅对括号内的数字求和,可以先提取括号内的数字,然后将它们相加。

1.在您要对其方括号中的数字求和的单元格旁边选择一个空白单元格,例如B2,输入此公式
=IF(ISERROR(MID(A1,FIND("(",A1)+1,(FIND(")",A1))-(FIND("(",A1)+1))),0,(MID(A1,FIND("(",A1)+1,(FIND(")",A1))-(FIND("(",A1)+1)))+0),
然后向下拖动自动填充手柄以从每个单元格中提取括号中的数字。 看截图:
括号中的文档总和编号2

Tips::在公式中,A1是要从中提取括号内数字的单元格。

2.选择一个将求和结果放在其中的单元格,输入 = SUM(B1:B8),然后按 输入 键。 看截图:
括号中的文档总和编号3

提示: 在公式中,B1:B8是您从文本字符串原始列表中提取并想要求和的数字。


方括号内的总和数仅适用于定义函数

您也可以将“定义的函数”应用于括号内的数字求和。

1。 按 Alt + F11键 启用键 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 模块,然后将以下代码粘贴到空白脚本中。

VBA:括号中的总和

Function SumBracket(Target As Range) As Double
'UpdatebyExtendoffice20160901
Dim xCell As Range
Dim xObjs As Object, xObj As Object
Dim xSum As Double
Set xObjs = CreateObject("VBScript.RegExp")
xSum = 0
With xObjs
    .Global = True
    .Pattern = "\((\d+(\.\d+)?)\)"

    For Each xCell In Target
      If xCell.Value <> "" Then
        For Each xObj In xObjs.Execute(xCell.Value)
            xSum = xSum + xObj.submatches(0)
        Next
      End If
    Next
End With
SumBracket = xSum
End Function

括号中的文档总和编号4

2.保存代码,然后选择要放置结果的单元格,然后输入此公式 = SumBracket(A1:A8), 按 输入 键。 看截图:
括号中的文档总和编号5


仅使用 Kutools for Excel 对括号内的数字求和

其实, Kutools for Excel提取文字 函数提取括号内的数字,然后应用 绝对值总和 函数对数字求和。

Kutools for Excel, 与超过 300 方便的功能,使您的工作更加轻松。 

安装后 Kutools for Excel,请执行以下操作:(立即免费下载 Kutools for Excel!)

1.在括号中选择要提取数字的字符串,然后单击 库图尔s> 文本 > 提取文字。 看截图:
括号中的文档总和编号6

2.在 提取文字 对话框中,输入 (*)文本 文本框,然后单击 地址 把它添加到 提取清单。 然后点击 Ok,然后选择一个单元格以将提取的数字放在弹出对话框中,例如C1。 看截图:

请注意: 如果还有其他提取规则 提取清单 部分,请取消选中这些提取规则,而仅检查新添加的提取规则 (*).


括号中的文档总和编号7 向右射箭 括号中的文档总和编号8

3。 点击 OK,现在提取了方括号中的数字。 默认情况下,括号内的数字被识别为负数,您需要将其绝对值相加。
括号中的文档总和编号9

4.选择一个要在其中放置计算结果的单元格,单击 库工具 > 公式助手 > 数学与三角学 > 绝对值总和。 看截图:
括号中的文档总和编号10

5.在 公式助手 对话框中,选择包含提取数字的范围 联系电话 文本框,在这种情况下为C1:C8。 请点击 Ok,已添加数字的绝对值。 看截图:

括号中的文档总和编号11 向右射箭 括号中的文档总和编号12

小费。 如果您想免费试用 提取文字总和绝对值的功能 请去免费下载 Kutools for Excel 首先,然后按照上述步骤进行操作。

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (15)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello Sunny,

I have a question as well. I have an excel sheet with numbers and some of them are in brackets. I want to add the values of the numbers in a row. Basically:
50 (50*) 100 150 = 350
How can I add (=sum) of all of them, including the one in brackets? I'ld appreciate your advise.
Best, Anna
This comment was minimized by the moderator on the site
Vielen Dank Sonnig,

jetzt ich sehe wo war das Problem. Sie schreiben im VBA und Zelle "." statt wie bei mir "," Ich habe im VBA diese Zeile:
.Pattern = "\((\d+(\.\d+)?)\)"
in diese:
.Pattern = "\((\d+(\,\d+)?)\)"
geändert und dann funktioniert alles perfekt.

Noch mall vielen, vielen Dank.
Schöne Grüße

Wojtek
This comment was minimized by the moderator on the site
...ich meinte addieren Dezimalbruche 🙂
This comment was minimized by the moderator on the site
Hi, Wojtek, if you just want to sum the decimal numbers only, try this code:
Function SumBracket(Target As Range) As Double
'UpdatebyExtendoffice20221209
Dim xCell As Range
Dim xObjs As Object, xObj As Object
Dim xSum As Double
Dim xNum As Integer
On Error Resume Next
Application.Volatile
Set xObjs = CreateObject("VBScript.RegExp")
xSum = 0
With xObjs
    .Global = True
    .Pattern = "\((\d+(\.\d+)?)\)"

    For Each xCell In Target
      If xCell.Value <> "" Then
        For Each xObj In xObjs.Execute(xCell.Value)
            xSum = xSum + Val(xObj.submatches(0)) - Int(Val(xObj.submatches(0)))
        Next
      End If
    Next
End With
SumBracket = xSum
End Function
This comment was minimized by the moderator on the site
Hallo Sonnig,
vielen Dank für die schnelle Antwort 😀. Um die Ganze klar zu machen gebe ich ein Beispiel meiner Aufgabe:

a(5), b(2,5), c(0,25) = 7,75

wie gesagt, mit:

a(5), b(2), c(21) im eine Zelle funktioniert die erste Makro super.

Vielen Dank und schöne Grüße
Wojtek
This comment was minimized by the moderator on the site
Sorry, Wojtek, I do undestand Geman, I just translate your question by Google so that I do not get your question clearly. Could you give me more description, for example, the data structure, are they in a cell or in different cell, in a column or in a row, what result you want.
This comment was minimized by the moderator on the site
Hallo Sonnig,

noch mal vielen Dank für die Mühe.

Geht um Summe alle Zahlen die in der Klammer stehen...
Erste Zelle: 458/1(2,5), 458/2(11), 458/3(0,75)
Zweite Zelle: Summe von 2,5 und 11 und 0,75
alles was in der Klammer steht in der erste Zelle sollte summiert sein. das Ergebnis soll in der Zweite Zelle erscheinen.

Vielen Dank und schöne Grüße
Wojtek
This comment was minimized by the moderator on the site
Hallo,
danke, Makro funktioniert super, aber leider nur mit Ganze Zahl, nicht mit Bruchzahl. Wäre das möglich zu ändern?
This comment was minimized by the moderator on the site
I'm sorry, I meant adding the numbers outside the bracket.
This comment was minimized by the moderator on the site
Hi, Adduar, I do not find a formula to solve your problem, maybe you only can use the Extract Text tool of Kutools for Excel to extract the numbers first, and then sum them, the conditions are *( and )*.
This comment was minimized by the moderator on the site
How to add numbers within the bracket?
This comment was minimized by the moderator on the site
Hi, How do I modify the pattern to sum everything between F-( and )


Example of my data is: F-(10) F-(12) E-(8)


the sum would be 22


thanks
This comment was minimized by the moderator on the site
Hi, Andy, please try this code:
Function SumBracket(Target As Range) As Double
'UpdatebyExtendoffice20160901
Dim xCell As Range
Dim xObjs As Object, xObj As Object
Dim xSum As Double
Set xObjs = CreateObject("VBScript.RegExp")
xSum = 0
With xObjs
.Global = True
.Pattern = "\-\((\d+(\.\d+)?)\)"

For Each xCell In Target
If xCell.Value <> "" Then
For Each xObj In xObjs.Execute(xCell.Value)
xSum = xSum + xObj.submatches(0)
Next
End If
Next
End With
SumBracket = xSum
End Function
This comment was minimized by the moderator on the site
Hello, is there a Google App Script version of the VBA script. I would like to use it in Google Sheets.
Thanks. :D
This comment was minimized by the moderator on the site
Sorry I do not know
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations