メインコンテンツへスキップ

Excelでフルパスからファイル名をすばやく抽出するにはどうすればよいですか?

ファイルパスのリストを含むワークシートがあるとすると、次のスクリーンショットのように、各パスからファイル名(最後の円記号の右側)だけを抽出します。 このタスクに対処するための簡単なトリックはありますか?

Excelの数式でフルパスからファイル名を抽出します
ユーザー定義関数を使用してフルパスからファイル名を抽出します
VBAコードを使用してフルパスからファイル名を抽出します


Excelの数式でフルパスからファイル名を抽出します

Excelでは、次の式を使用して、フルパスからファイル名のみをすばやく抽出できます。

空白のセルを選択し、以下の数式を入力して、を押します。 入力します キー。

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

ヒント:A1は、ファイル名を抽出するセルです)、Enterボタンを押してから、塗りつぶしハンドルをドラッグして、目的の塗りつぶし範囲にします。 その後、各セルからファイル名が抽出されます。

ドキュメント抽出名1


ユーザー定義関数を使用してフルパスからファイル名を抽出します

次のユーザー定義関数を使用すると、ファイル名を簡単かつ迅速に取得できます。

1。 を押し続けます Alt + F11 キーを押して Microsoft Visual Basic forApplicationsウィンドウ.

2に設定します。 OK をクリックします。 インセット > モジュール、次のコードをに貼り付けます モジュールウィンドウ.

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)

doc-extract-ファイル名1

次に、セルB1を再度選択し、塗りつぶしハンドルをこの数式を適用する範囲にドラッグすると、次の結果としてすべてのファイル名がフルパスから抽出されます。

doc-extract-ファイル名1


VBAコードを使用してフルパスからファイル名を抽出します

ユーザー定義関数に加えて、VBAコードはファイル名の抽出にも役立ちます。 このようにしてください:

1。 を押し続けます Alt + F11 キーを押して Microsoft Visual Basic forApplicationsウィンドウ.

2に設定します。 OK をクリックします。 インセット > モジュール、モジュールウィンドウに次のコードを貼り付けます。

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 このコードを実行するためのキーを押し、ファイル名を抽出する範囲を選択します。スクリーンショットを参照してください。

doc-extract-ファイル名1

4。 そして、 OK、ファイル名は次のように選択から抽出されています。

Note:このVBAコードを使用すると、元のデータが破棄されるため、このコードを適用する前にXNUMXつコピーする必要があります。

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 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