Name Asステートメントを使うと、Excel以外の他のファイルの名前を変更することができます。
Name ASステートメントの使い方
ファイル名の変更・ファイルの移動
Name パス/変更前のファイル名 As 出力するパス/変更後のファイル名
Name パス/変更前のファイル名 As 出力するパス/変更後のファイル名

名前を変更するためのファイルを用意しておきます。

文法的にはNameの後に元のファイル名、asを挟んで変更後のファイル名となります。
ファイル名だけで実行すると、
ファイル名だけで実行すると、

パスにはカレントフォルダが指定され、特に何もしてなければこのようにエラーになります。

というわけで、パスを指定してあげます。手打ちするかファイルのプロパティなどからパスをコピーし、

ファイル名の前に付けて、\でつなげます。
Name “(パス)\abc.txt” As “(パス)\123.txt”というコードになります。
Name “(パス)\abc.txt” As “(パス)\123.txt”というコードになります。

実行すると、指定のファイルの名前が変更されていることが確認できます。
Excelと同じフォルダのパスを取得

実用性を考えると、パスもコードで取得できたほうがいい場合があります。Excelの現在地であればThisWorkbook.Pathでパスが取得できるので、

パス名の代わりにこれを挿入し、Name ThisWorkbook.Path & “\abc.txt” As ThisWorkbook.Path & “\123.txt”のようにすれば、Excelと同じフォルダの指定したファイルの名前を変更できます(ファイル名の頭に\を付けること)。
別のフォルダに移動

変更前のパスと変更後のパスをちがうものにした場合、

名前を変更しつつ、ファイルが移動します。

同じファイル名でも問題ないので、移動目的で使用することも可能です。
なお、Name Asステートメント(というかファイル操作系全般)は実行中のファイルに対しては実行できません。

Excel(エクセル):ファイル、ブック
Excelのファイルやブックにまつわる操作まとめです。広義で言えばすべての操作がそうなんですが、シートはシート、セルはセルで一応分割しています。
ファイル・ブック関連操作
Excelファイル(ブック)を新しく作る方法です。
...

やりたいことから方法を探すエクセル(Excel)操作・関数・VBA(マクロ)逆引きまとめ
逆引き(やりたいことから探す)Excel記事まとめ

関数・演算子・メソッド・プロパティ名から探すExcel/VBA(マクロ)使い方・組み合わせ方まとめ
こちらはExcelやメソッドの諸機能を、機能の名称から探せるまとめ記事です。
コメント