跳到主要內容

如何僅在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

尖端:在公式中,A1是要從中提取括號內數字的單元格。

2.選擇一個將求和結果放在其中的單元格,輸入 = SUM(B1:B8),然後按 Enter 鍵。 看截圖:
括號中的文檔總和編號3

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


方括號內的總和數僅適用於定義函數

您也可以將“定義的函數”應用於括號內的數字求和。

1。 按 Alt + F11鍵 啟用鍵 Microsoft Visual Basic for Applications 窗口中,單擊 插入 > 模塊,然後將以下代碼粘貼到空白腳本中。

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), 按 Enter 鍵。 看截圖:
括號中的文檔總和編號5


括號內的數字總和僅適用於Kutools for Excel

其實, Excel的Kutools提取文字 函數提取括號內的數字,然後應用 絕對值總和 函數對數字求和。

Excel的Kutools, 與以上 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選項卡為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