この記事では、Excel VBAを使用してファイル名に重要度や優先度を追加する方法について詳しく説明します。具体的なコードの例、その詳細な解説、さらには応用例までを含めて紹介します。この情報を元に、ファイル管理の効率化を目指しましょう。
目次
基本のコード
まずは、基本的なコードから始めます。このコードは、現在開いているExcelファイルの名前の後ろに「_重要」を追加して保存します。
Sub RenameFileWithPriority()
Dim CurrentName As String
Dim NewName As String
CurrentName = ThisWorkbook.FullName
NewName = Replace(CurrentName, ".xlsx", "_重要.xlsx")
ThisWorkbook.SaveAs NewName
End Sub
コードの詳細解説
1. `CurrentName`という変数を用意して、現在のExcelファイルのフルパスを取得します。
2. `NewName`という変数を用意して、`CurrentName`の.xlsxの部分を”_重要.xlsx”に置き換えます。これで新しい名前が完成します。
3. `ThisWorkbook.SaveAs NewName`で新しい名前で保存します。
応用例
1. 優先度に応じてファイル名を変更する
この応用例では、ユーザーに優先度を入力させ、それに応じてファイル名を変更します。
Sub RenameFileBasedOnPriority()
Dim Priority As String
Dim CurrentName As String
Dim NewName As String
Priority = InputBox("優先度を入力してください(高/中/低)")
CurrentName = ThisWorkbook.FullName
NewName = Replace(CurrentName, ".xlsx", "_" & Priority & ".xlsx")
ThisWorkbook.SaveAs NewName
End Sub
2. 日付を追加して保存する
特定のファイルが何日に編集されたのかを追跡する場合に便利です。
Sub RenameFileWithDate()
Dim CurrentName As String
Dim NewName As String
CurrentName = ThisWorkbook.FullName
NewName = Replace(CurrentName, ".xlsx", "_" & Format(Now(), "yyyymmdd") & ".xlsx")
ThisWorkbook.SaveAs NewName
End Sub
3. ファイル名を完全にカスタマイズする
ユーザーに任意のテキストを入力させ、それをファイル名に使用します。
Sub CustomRenameFile()
Dim CustomText As String
Dim CurrentName As String
Dim NewName As String
CustomText = InputBox("新しいファイル名の追加部分を入力してください")
CurrentName = ThisWorkbook.FullName
NewName = Replace(CurrentName, ".xlsx", "_" & CustomText & ".xlsx")
ThisWorkbook.SaveAs NewName
End Sub
まとめ
Excel VBAを使用して、ファイル名に重要度や優先度、さらには日付やカスタムテキストを追加する方法を紹介しました。これらのテクニックを利用することで、ファイル管理をより効率的に行うことができます。
コメント