予告通り、今回は指定フォルダ内のファイルをリネームするExcel VBAを公開します。このVBAは、以前の記事で紹介したExcel VBAと組み合わせて使うことを想定しています。
How to Use/使い方
Write a new name in advance/予め新しいファイル名を書き込んでおく
Before you activate this macro, write a new name of the file whose name you want to change into a cell that located right of the current-name cell by a single cell.
本マクロ起動前に、変更したいファイル名の一つ右の列に、変更後のファイル名を記入しておきます。
Inputting the cell name in which a folder address is written/フォルダアドレスが書き込まれているセル名を入力
Rename of file is done/ファイルのリネーム完了
You see Hyperlink for an older name is deleted and Hyperlink for a new name is added.
旧ファイル名へのハイパーリンクは削除され、新しいファイル名へのリンクが形成されています。
You see file name renamed.
ファイルがリネームされていることが確認できます。
コード
お願い
おそらくコードを埋め込むプラグインSyntaxHighlighterの仕様もしくは設定によって、コード貼り付けた際に “&” が”&“と表示されてしまうようです。
HTMLだとそうなっちゃうの??このあたりよくわかってません。
現状、解決策がわかっていないので、大変お手数ですが、コードをコピペするときは、事前に”&“を”&“に置換してください。
PluginをEnlighter – Customizable Syntax Highlighterに変更しました。
コピペだけで使っていただけると思います。
Sub RenameFile() Dim FolderPath As String 'address of a target folder Dim FolderPathCell As Range 'cell in which folder path is stored Dim file As Object Dim i As Integer Dim TargetCell As Range Dim NewNameCell Dim Row As Integer Dim Column As Integer Dim FSO As Object 'refer to a cell designated by a user Set FolderPathCell = Range(InputBox("Input a cell showing Folder path")) FolderPath = FolderPathCell.Text Set FSO = CreateObject("Scripting.FileSystemObject") Set TargetCell = Cells(FolderPathCell.Row + 1, FolderPathCell.Column) Row = TargetCell.Row Column = TargetCell.Column For i = 0 To 100 Set TargetCell = Cells(Row + i, Column) Set NewNameCell = Cells(Row + i, Column + 1) If TargetCell = "" Then Exit For End If If NewNameCell <> "" Then Set file = FSO.getfile(FolderPath & "\" & TargetCell.Text) file.Name = NewNameCell.Text 'rename TargetCell.Hyperlinks.Delete 'delete old hyperlink NewNameCell.Hyperlinks.Add anchor:=NewNameCell, Address:=FolderPath & "\" & NewNameCell.Text End If Next End Sub
最近のコメント