跳到主要内容

如何快速从Excel中的完整路径提取文件名?

假设您有一个包含文件路径列表的工作表,现在您只想从每个路径中提取文件名(最后反斜杠的右侧),如下面的屏幕快照所示。 是否有任何快速技巧来处理此任务?

使用Excel中的公式从完整路径中提取文件名
使用用户定义函数从完整路径中提取文件名
使用VBA代码从完整路径中提取文件名


使用Excel中的公式从完整路径中提取文件名

在Excel中,您可以使用以下公式快速从完整路径中仅提取文件名。

选择一个空白单元格,在其中输入以下公式,然后按 输入 键。

=MID(A1,FIND("*",SUBSTITUTE(A1,"\","*",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))

Tips:A1是要从中提取文件名的单元格,然后按Enter按钮,然后拖动填充手柄以填充所需的范围。 之后,从每个单元格中提取文件名。

doc提取名称1


使用用户定义函数从完整路径中提取文件名

使用以下用户定义函数,您可以轻松快捷地获取文件名。

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

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

Function FunctionGetFileName(FullPath As String) As String
'Update 20140210
Dim splitList As Variant
splitList = VBA.Split(FullPath, "\")
FunctionGetFileName = splitList(UBound(splitList, 1))
End Function

3.其他 + Q 关闭键 Microsoft Visual Basic应用程序 窗口,然后返回工作表。 在空白单元格(例如B1)中,输入以下公式,然后按 输入 键。

=FunctionGetFileName(A1)

文档提取文件名1

然后再次选择单元格B1,将填充手柄拖到您要应用此公式的范围,并且所有文件名都已从完整路径中提取出来,如下所示:

文档提取文件名1


使用VBA代码从完整路径中提取文件名

除了用户定义功能外,VBA代码还可以帮助您提取文件名。 请这样做:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到“模块窗口”中。

Sub GetFileName()
'Update 20140210
Dim Rng As Range
Dim WorkRng As Range
Dim splitList As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    splitList = VBA.Split(Rng.Value, "\")
    Rng.Value = splitList(UBound(splitList, 1))
Next
End Sub

3。 然后按 F5 键运行此代码,然后选择要从中提取文件名的范围,请参见屏幕截图:

文档提取文件名1

4。 然后点击 OK,文件名已从选择中提取出来,如下所示:

备注:使用此VBA代码,原始数据将被销毁,因此您应在应用此代码之前复制一个数据。

最佳办公生产力工具

🤖 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 (2)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Very useful function. Is there a way to modify the script so it finds the filename if the path has either \ or / slashes?

I have a spreadsheet with various hyperlinks where the underlying paths are either \ or / (back slash or forward slash) separates - I think its because some of the links to files were done just as bookmarks in Word originally, or to files on an internal doc server. Or maybe its because some path links are made with absolute vs relative path links?

eg:

../../../../Documents/2ndQuarter/2019/standardcost_widget12345.pdf
or
\fileserver\factory23\Operations\Parts_Mgt\Documents\2ndQuarter\2019\standardcost_widget12345.pdf


When I ran the getfilename function, it got all the filenames that were in paths with \ between directories or folders, but the links with / slashes were returned as is.
I altered & added a second function that was similar but replaced the "\" in line 4 with "/" and called it forwardslashgetfilename and run it in a separate column after I run the first function.

Running one function after another is not difficult, but I was curious if you can expand the code in splitList operation in line 4 to include both "\" or "/". I'm no VBA programmer but I tried splitList = VBA.Split(FullPath, "\" or "/") and it didn't work.

Thoughts? I'm assuming its some simple syntax - I'm just clueless at this point... but I will start poking around the interwebs...

Tks!
This comment was minimized by the moderator on the site
The formula raises a #VALUE! error if the source cell has only a filename to begin with.. Embedding the entire formula in an IFERROR function solves this problem, e.g., =IFERROR(<orig formula>,A1)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations