跳到主要內容

計算單元格中逗號分隔的值

在本教程中,您將學習如何使用LEN,TRIM和SUBSTITUTE函數的組合來計算Excel中單元格中逗號分隔的值。


如何計算Excel中單元格中逗號分隔的值?

如下面的屏幕截圖所示,在B列中有一個逗號分隔的字符串列表,您要計算每個單元格中的逗號分隔值,請執行以下操作來實現。

通用公式

=IF(ISBLANK(B3),"",LEN(TRIM(B3))-LEN(SUBSTITUTE(TRIM(B3),",",""))+1)

參數

A1:代表包含逗號分隔字符串的單元格,您將計算逗號分隔值。

如何使用這個公式?

1.選擇一個空白單元格以輸出結果。 在這種情況下,我選擇單元格D3。

2.輸入以下公式,然後按 Enter 鍵。 然後將其“填充手柄”一直向下拖動,以將其應用於其他單元格。

=IF(ISBLANK(B3),"",LEN(TRIM(B3))-LEN(SUBSTITUTE(TRIM(B3),",",""))+1)

備註:在此公式中,B3是包含逗號分隔的字符串的單元格,您將在其中計算逗號分隔的值。 您可以根據需要進行更改。

這個公式是如何工作的?

=IF(ISBLANK(B3),"",LEN(TRIM(B3))-LEN(SUBSTITUTE(TRIM(B3),",",""))+1)

1. LEN(TRIM(B3)):TRIM函數從逗號分隔的字符串“ AA,BB,CC,DD”中刪除所有提取空格,並且僅在單詞之間保留單個空格。 然後LEN函數計算“ AA,BB,CC,DD”的總長度,並返回結果14。

2. LEN(SUBSTITUTE(TRIM(B3),",",""))

  • SUBSTITUTE(TRIM(B3),",",""):如上面的說明所示,TRIM函數在此處從“ AA,BB,CC,DD”中刪除所有提取空間,並將結果返回為“ AA,BB,CC,DD”。 然後,SUBSTITUTE函數將“ AA,BB,CC,DD”中的所有逗號全部替換為空,並得到結果“ AA BB CC DD”;
  • LEN("AA BB CC DD"):LEN函數返回文本字符串“ AA BB CC DD”中的字符數。 結果是11。

3. 14-11 1 +:在這裡,使用“ AA,BB,CC,DD”的總長度減去“ AA BB CC DD”的總長度,得出字符串中的逗號總數(14-11 = 3)。 由於最後一個逗號後面總是有一個單詞,因此您需要在數字1上加3以獲取字符串中逗號分隔的單詞總數。 結果是4。

4. 如果(ISBLANK(B3),“”,14):這裡的 If 函數表示如果 B3 為空,則什麼都不返回,否則返回數字 14。這裡 B3 包含值,因此 IF 函數返回數字 14。


相關功能

Excel LEN功能
Excel LEN函數返回文本字符串中的字符數。

Excel SUBSTITUTE功能
Excel SUBSTITUTE函數將文本字符串中的文本或字符替換為另一個文本或字符。

Excel TRIM功能
Excel TRIM函數從文本字符串中刪除所有多餘的空格,並且僅在單詞之間保留單個空格。


相關公式

計算Excel單元格中特定字符的出現
本教程介紹如何應用基於LEN和SUSTITUTE函數的公式來計算特定字符在Excel單元格中出現的次數。

計算單元格範圍內的特定字符
本教程介紹一些公式,以計算特定字符在Excel範圍內出現的次數。

計算Excel中單元格中的特定單詞
本文將介紹公式,以計算特定單詞在Excel單元格中出現的次數。

計算Excel中範圍內的特定單詞
本文介紹了一種用於計算特定單詞在Excel中單元格區域中出現的次數的公式。

計算Excel中單元格中的字符數
總字符不僅包括所有字母,還包括單元格中的所有空格,標點符號和符號。 按照本教程操作,可以使用Excel中的LEN函數輕鬆地計算單元格中的總字符數。

計算Excel中範圍內的字符數
本文介紹了一個公式,用於計算Excel中一系列單元格中出現的總字符數。


最佳辦公效率工具

Kutools for Excel-幫助您從人群中脫穎而出

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項  |  刪除空白行  |  合併列或儲存格而不遺失數據  |  沒有公式的回合 ...
超VLookup: 多重標準  |  多重價值  |  跨多頁  |  模糊查詢...
副詞。 下拉清單: 簡易下拉列表  |  依賴下拉列表  |  多選下拉列表...
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  將列與 選擇相同和不同的單元格 ...
特色功能: 網格焦點  |  設計圖  |  大方程式酒吧  |  工作簿和工作表管理器 | 資源庫 (自動文字)  |  日期選擇器  |  合併工作表  |  加密/解密單元格  |  按清單發送電子郵件  |  超級濾鏡  |  特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符 ...)  |  50+ 圖表 類型 (甘特圖 ...)  |  40+ 實用 公式 (根據生日計算年齡 ...)  |  19 插入 工具 (插入二維碼, 從路徑插入圖片 ...)  |  12 轉化 工具 (數字到單詞, 貨幣兌換 ...)  |  7 合併與拆分 工具 (高級合併行, 拆分 Excel 儲存格 ...)  |  ... 和更多

Kutools for Excel 擁有超過 300 個功能, 確保只需點擊一下即可獲得您所需要的...

產品描述


Office選項卡-在Microsoft Office(包括Excel)中啟用選項卡式閱讀和編輯

  • 一秒鐘即可在數十個打開的文檔之間切換!
  • 每天為您減少數百次鼠標單擊,告別鼠標手。
  • 查看和編輯多個文檔時,將您的工作效率提高 50%。
  • 為 Office(包括 Excel)帶來高效的選項卡,就像 Chrome、Edge 和 Firefox 一樣。
Comments (6)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Thanks for this formula. I had been trying a different formula: =LEN(A1)-LEN(SUBSTITUTE(A1),",",""))+1 but if the cell was blank it would have a 1 in it. This doesn't, so it works for counting up numbers. In my case, I'm trying to count the number of dates such as Oct. 6, 13, 20, 27. However, it's not working when it comes to multiplying the count by the cost of the lesson. For instance, if John had lessons on Oct. 6, 13, 20, 27, I have a column for dates of lessons, then a column for number of lessons, a column for the cost of each lesson (30 or 45), and a column to calculate monthly tuition due (number of lessons x cost). For the student with blank dates/no lessons, it gives an error value, and therefore affects my total tuition for the month. Please advise. Thanks!
This comment was minimized by the moderator on the site
Hi sir, formula is not working properly. I have numerical data in cells. It works correctly in cells where numbers consists upon two digits like 22,43,59 etc, but it fails when there comes data like 103,144 or 165,45 etc. Please help
This comment was minimized by the moderator on the site
Hi,
The formula works fine for me and do not reproduce the error you mentioned. May I ask what version of Excel you are using?
This comment was minimized by the moderator on the site
Office 2021
This comment was minimized by the moderator on the site
thanks! this worked very well, except for that it also returns "1" when the cell is blank. I'm trying to count the number of concatenated dates. There are some cells that don't have any dates, and those are coming back with the number '1'. However, the formula is still counting correctly the number of dates otherwise.
This comment was minimized by the moderator on the site
Hi Alicia Majcher,
Thank you for your feedback. If you want to return nothing when the reference cell is blank, apply the following formula.
=IF(ISBLANK(A30),"",LEN(TRIM(A30))-LEN(SUBSTITUTE(TRIM(A30),",",""))+1)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations