跳到主要内容

如何从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个单词!


Kutools for Excel - 使用 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's 分裂细胞 实用程序从文本字符串单元格提取每个单词,然后根据需要水平或垂直列出提取的单词。

Kutools for Excel - 包含 300 多个 Excel 基本工具。 享受全功能 30 天免费试用,无需信用卡! 现在下载!

1。 选择您将从中提取单词的文本字符串单元格,然后单击 库工具 > 合并与拆分 > 分裂细胞.

2。 在打开的“拆分单元格”对话框中,在 Type 部分,检查 太空 在选项 指定分隔符 部分,然后单击 Ok 按钮。 看截图:

3。 现在指定要将提取的单词输出到的目标范围,然后单击 OK 按键

如果您检查了 拆分为列 在上面的“拆分单元格”对话框中的“选项”中,每个单词都将从每个文本字符串中提取出来并垂直列出。

如果您检查了 拆分为行 在上面的“拆分单元格”对话框中的“选项”中,将从每个文本字符串中提取每个单词并水平列出。


使用出色的工具从Excel中的文本字符串中提取第n个单词

如果您已经安装了 Kutools for Excel,则可以使用其 Formula Helper > 提取单元格中的第 n 个单词功能轻松快速地从指定单元格中提取第 n 个单词。

Kutools for Excel - 包含 300 多个 Excel 基本工具。 享受全功能 30 天免费试用,无需信用卡! 现在下载!

1。 选择将提取的单词放在其中的单元格,然后单击 库工具 > 公式助手 > 公式助手 启用此功能。

2。 在“公式帮助器”对话框中,请执行以下操作:
(1)选择 文本 来自 配方类型 下拉列表;
(2)单击以突出显示 提取单元格中的第n个单词 ,在 选择一个公式 列表框;
(3)在 手机 框中,指定要从中提取单词的单元格;
(4进 第N个 框中,指定编号。

3。 点击 Ok 按钮。 如有必要,请拖动公式单元格的“自动填充手柄”,然后将公式复制到其他单元格中。


演示:从文本字符串中提取每个单词,然后水平或垂直列出


Kutools for Excel:超过 300 个方便的工具触手可及! 立即开始 30 天免费试用,没有任何功能限制。 立即下载!

相关文章:

最佳办公生产力工具

🤖 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 (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