跳到主要內容

如何從Excel中的文本字符串中提取第一個/最後一個/第n個單詞?

您是否曾經遇到過需要從工作表中的文本字符串中提取特定單詞的問題? 例如,您需要以下範圍的文本字符串,才能從中獲得第一個/最後一個或第n個單詞,在這裡,我可以討論一些有效的方法來解決該問題。


使用公式從文本字符串中提取名字或姓氏

如果您需要從文本字符串列表中提取第一個單詞,則以下公式可以為您提供幫助。

要提取第一個單詞,請在下面的公式中輸入一個空白單元格,然後根據需要將此單元格的“自動填充”手柄拖動到範圍內。

= IF(ISERR(FIND(“”,A2)),“”,LEFT(A2,FIND(“”,A2)-1))

要從每個單元格中提取最後一個單詞,請應用以下公式:

= IF(ISERR(FIND(“”,A2)),“”,RIGHT(A2,LEN(A2)-FIND(“ *”,SUBSTITUTE(A2,“”,“ *”,LEN(A2)-LEN( SUBSTITUTE(A2,“”,“”)))))))

現在,您將看到從每個單元格中提取了第一個或最後一個單詞。

筆記: 在上述公式中 A2 表示要從中提取第一個或最後一個單詞的單元格。

很難記住長而復雜的公式嗎? 驚人的工具可幫助您提取第n個單詞w僅點擊幾下!

上面的長公式只能提取第一個和最後一個單詞,但是對提取指定的第n個單詞,第二個單詞,第六個單詞等表示提取將無用。即使您可以找出解決問題的公式,這些公式也必須太複雜而無法記憶和應用。 在這裡,建議使用Kutools for Excel的“在單元格中提取第n個單詞”功能,它將幫助您盡可能輕鬆地提取第n個單詞!


Excel的Kutools - 使用 300 多種基本工具增強 Excel 功能。 享受全功能 30 天免費試用,無需信用卡! 立即行動吧!

使用用戶定義函數從文本字符串中提取第n個單詞

如果要從文本字符串中提取第二,第三或第n個單詞,則可以創建一個用戶定義的函數來處理它。

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications窗口.

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

Function FindWord(Source As String, Position As Integer)
'Update 20131202
Dim arr() As String
arr = VBA.Split(Source, " ")
xCount = UBound(arr)
If xCount < 1 Or (Position - 1) > xCount Or Position < 0 Then
    FindWord = ""
Else
    FindWord = arr(Position - 1)
End If
End Function

3。 然後保存代碼,在此示例中,我將從字符串中獲得第三個單詞,因此鍵入此公式 = findword(A2,3) 放入空白單元格B2中,然後根據需要將該單元格的“自動填充”手柄拖動到範圍內。 看截圖:

注意: 在上式中 A2 是您要從中提取單詞的單元格,數字 3 表示您將提取的字符串中的第三個單詞,您可以根據需要進行更改。


從文本字符串中提取每個單詞,然後水平或垂直列出

該方法將介紹Kutools for Excel的 分裂細胞 實用程序從文本字符串單元格提取每個單詞,然後根據需要水平或垂直列出提取的單詞。

Excel的Kutools - 包含 300 多個 Excel 基本工具。 享受全功能 30 天免費試用,無需信用卡! 現在下載!

1。 選擇您將從中提取單詞的文本字符串單元格,然後單擊 庫工具 > 合併與拆分 > 分裂細胞.

2。 在打開的“拆分單元格”對話框中,在 類別 部分,檢查 宇宙 在選項 指定分隔符 部分,然後單擊 Ok 按鈕。 看截圖:

3。 現在指定要將提取的單詞輸出到的目標範圍,然後單擊 OK 按鍵

如果您檢查了 拆分為列 在上面的“拆分單元格”對話框中的“選項”中,每個單詞都從每個文本字符串中提取出來並垂直列出。

如果您檢查了 拆分為行 在上面的“拆分單元格”對話框中的“選項”中,將從每個文本字符串中提取每個單詞並水平列出。


使用出色的工具從Excel中的文本字符串中提取第n個單詞

如果您已安裝Kutools for Excel,則可以使用其公式助手>提取單元格中的第n個單詞來輕鬆快速地從指定的單元格中提取第n個單詞。

Excel的Kutools - 包含 300 多個 Excel 基本工具。 享受全功能 30 天免費試用,無需信用卡! 現在下載!

1。 選擇將提取的單詞放在其中的單元格,然後單擊 庫工具 > 公式助手 > 公式助手 啟用此功能。

2。 在“公式幫助器”對話框中,請執行以下操作:
(1)選擇 文本 來自 配方類型 下拉列表;
(2)單擊以突出顯示 提取單元格中的第n個單詞 ,在 選擇一個公式 列錶框;
(3)在 細胞 框中,指定要從中提取單詞的單元格;
(4)在 第N個 框中,指定編號。

3。 點擊 Ok 按鈕。 如有必要,請拖動公式單元格的“自動填充手柄”,然後將公式複製到其他單元格中。


演示:從文本字符串中提取每個單詞,然後水平或垂直列出


Excel的Kutools:超過 300 個方便的工具觸手可及! 立即開始 30 天免費試用,沒有任何功能限制。 現在就下載!

相關文章:

最佳辦公生產力工具

🤖 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 (38)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I've created the FindWord custom function as described and it worked perfectly but it stopped working the next day. I looked in the VBA window and the Module is still there. Any idea why the function is not working anymore?
This comment was minimized by the moderator on the site
Dark Chocolate 25gm box 12 pcs

Dark Chocolate 20gm*24 box

White Chocolate 15gm

White Chocolate 25gm*24

Biscuits W/Marshmallow300gm

Chocolate 40gm

Can some plz help to extract the numbers before "gm", for example : 25,20,15,25,300,40
This comment was minimized by the moderator on the site
Hi I would like to know how to use the VBA model but I have sentences of only one word, and i would like to pick the first word even if there is only one word.

Thank you
This comment was minimized by the moderator on the site
Hi Maura,
The VBA cannot extract the first word if there is only word in the cell. However, it’s recommended to apply the Text to Column to extract the first word of every cell in a column quickly.
1. Select the column, copy, and paste in a blank column.
2. Keep the new column selected, click Data > Text to Column.
3. In the Text column dialog, select Delimited, and then check Space as delimiters, and finally click Finish.
4. Now all words are separated by space. For the extracted words, you can remove all columns except the first one.
This comment was minimized by the moderator on the site
i want to remove the last word in cell and data is like this "/Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf/Cdtr/CtctDtls/FaxNb" can i have formula
This comment was minimized by the moderator on the site
Thanks so much for these! I have a question, though: how do I extract a number or group of numbers from a cell or textbox? For instance, if I have [37.5" x 21'] in cell A1, how could extract 37.5 into A2? There could be any number of characters and numbers in A1, but I'll always want the first number to the left of the "X", and the first number to the right. Not sure if you've covered something like this elsewhere, so I thought I'd ask here. (Not sure if this went through the first time, so here it is again.)
This comment was minimized by the moderator on the site
Thank you! It works fine called in a Sub.
This comment was minimized by the moderator on the site
the original formula isn't working for me, I keep getting #NAME?, anyone able to help? I am using Excel 2013 and this VBA would save me sooooo much time
This comment was minimized by the moderator on the site
The original VBA code posted here would save me so much time, however whenever I type in the =findword formula, I then get #NAME?, anyone able to help me? I am using Excel 2013
This comment was minimized by the moderator on the site
Hi, I am having cell with values like SAN_UN_TC1,SAN_UN_TC2,PEP_HR_TC1 I would like to extract first words liek SAN /PEP into one cell and words liek UN/HR to another cell How could i do it, Any help is appreciated. Thanks, Shiva
This comment was minimized by the moderator on the site
If number of characters in string (SAN_UN_TC1) is fixed (3_2_3), it's simple: column 1: =LEFT(B15,3) column 2: =MID(B15,5,2) column 3: =RIGHT(B15,3)
This comment was minimized by the moderator on the site
Many thanks for sharing VBA code....saved me a lot of time
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations