跳到主要內容

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

假設您有一個包含文件路徑列表的工作表,現在您只想從每個路徑中提取文件名(最後反斜杠的右側),如下面的屏幕快照所示。 是否有任何快速的技巧來處理此任務?

使用Excel中的公式從完整路徑中提取文件名
使用用戶定義函數從完整路徑中提取文件名
使用VBA代碼從完整路徑中提取文件名


使用Excel中的公式從完整路徑中提取文件名

在Excel中,您可以使用以下公式快速從完整路徑中僅提取文件名。

選擇一個空白單元格,在其中輸入以下公式,然後按 Enter 鍵。

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

保養竅門: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 for Applications 窗口,然後返回工作表。 在空白單元格(例如B1)中,輸入以下公式,然後按 Enter 鍵。

=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選項卡為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