LoginSignup
10

More than 5 years have passed since last update.

excel & VBAでファイル名を一括リネーム

Last updated at Posted at 2016-11-10
  1.  エクセルファイルのA列に「元のファイル名」B列に「変更後のファイル名」を入力しておき、下記サンプルではデスクトップに「myfolder」というフォルダを作成してある想定です。
  2.  ファイル右クリックから、プロパティでファイルパスを取得し、ファイルの親パスを指定する
  3.  実行するとA列の行数分実行されます
  4. 注:リネーム後の同名のファイルがあるとエラーが出ます。また、リネーム対象のファイルが無い場合もエラーが出ます

エクセルファイルのA列、B列

A B
aaa.pdf A-001.pdf
bbb.pdf A-002.pdf
ccc.pdf A-003.pdf
ddd.pdf A-004.pdf
eee.pdf A-005.pdf

デスクトップのmyfolderというフォルダには以下のファイルがある想定

→ aaa.pdf, bbb.pdf, ccc.pdf, ddd.pdf, eee.pdf

Visual Basicに標準モジュールを追加して以下を入力し実行

Option Explicit
Sub renamex()
    '保存先
    Const MyFolder$ = "C:\Users\●●●\Desktop\myfolder\"
    Const A& = 1, B& = 2

    Dim con As Long
    con = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To con
        Name MyFolder & Cells(i, A).Value As MyFolder & Cells(i, B).Value
    Next i
End Sub

ThisWorkbook.Pathでパスを取得する場合

エクセルファイルと同階層に、リネーム対象ファイルがある想定

Sub renamex()
    '保存先
    MyFolder$ = ThisWorkbook.Path & "\"
    Const A& = 1, B& = 2 

    Dim con As Long
    con = Cells(Rows.Count, A).End(xlUp).Row
    Dim i As Long
    For i = 1 To con
      Name MyFolder & Cells(i, A).Value As MyFolder & Cells(i, B).Value
    Next i
End Sub

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10