Excel VBAを用いて、数字を含むファイル名の数字部分をインクリメントする方法をご紹介いたします。この処理は、ファイル管理の際に、連番を持つファイルを扱うシチュエーションに非常に役立ちます。具体的なコード例、その詳細な解説、さらに応用例を通して、この手法の多岐にわたる利用シーンを深堀りしてまいります。
Excel VBAの基本
Excel VBA(Visual Basic for Applications)は、Microsoft Excelに組み込まれたプログラミング言語です。これを用いると、単純作業の自動化だけでなく、高度なデータ分析やレポート作成も可能になります。
そもそも、どこにVBAコードを書いて、どう実行すれば良いのか分からない場合は、以下の記事をご参照ください。

VBAを用いたファイル名の数字部分のインクリメント方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Function IncrementFileName(ByVal originalName As String) As String Dim regex As Object Dim matches As Object Dim numberPart As String Dim incrementedNumber As Long '正規表現を用いて数字部分を取得 Set regex = CreateObject("VBScript.RegExp") regex.Global = True regex.Pattern = "\d+" Set matches = regex.Execute(originalName) '数字部分が存在しない場合は、そのままのファイル名を返す If matches.Count = 0 Then IncrementFileName = originalName Exit Function End If '数字部分をインクリメント numberPart = matches(0).Value incrementedNumber = CLng(numberPart) + 1 IncrementFileName = regex.Replace(originalName, incrementedNumber) End Function |
コードの詳細解説
この関数は、指定されたファイル名(originalName
)の中に含まれる数字部分をインクリメントするためのものです。
1. 最初にVBScriptの正規表現オブジェクトを作成します。これを使用して、ファイル名に含まれる数字部分を抽出します。
2. 次に、正規表現のパターンを\d+
(1つ以上の数字)として設定し、originalName
から数字部分を抽出します。
3. 数字部分が存在しない場合、インクリメントは行わず、元のファイル名をそのまま返します。
4. 存在する場合、その数字部分をインクリメントし、元のファイル名の数字部分をインクリメントした数字に置き換えて返します。
応用例1:ファイル名の最後の数字部分のみをインクリメント
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Function IncrementLastNumberInFileName(ByVal originalName As String) As String Dim regex As Object Dim matches As Object Dim numberPart As String Dim incrementedNumber As Long '正規表現を用いて数字部分を取得 Set regex = CreateObject("VBScript.RegExp") regex.Global = True regex.Pattern = "\d+" Set matches = regex.Execute(originalName) '数字部分が存在しない場合は、そのままのファイル名を返す If matches.Count = 0 Then IncrementLastNumberInFileName = originalName Exit Function End If '最後の数字部分をインクリメント numberPart = matches(matches.Count - 1).Value incrementedNumber = CLng(numberPart) + 1 IncrementLastNumberInFileName = Replace(originalName, numberPart, incrementedNumber) End Function |
応用例2:特定の位置の数字部分のみをインクリメント
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Function IncrementSpecificNumberInFileName(ByVal originalName As String, ByVal position As Integer) As String Dim regex As Object Dim matches As Object Dim numberPart As String Dim incrementedNumber As Long '正規表現を用いて数字部分を取得 Set regex = CreateObject("VBScript.RegExp") regex.Global = True regex.Pattern = "\d+" Set matches = regex.Execute(originalName) '指定された位置に数字部分が存在しない場合、または数字部分が存在しない場合は、そのままのファイル名を返す If matches.Count < position Or matches.Count = 0 Then IncrementSpecificNumberInFileName = originalName Exit Function End If '指定された位置の数字部分をインクリメント numberPart = matches(position - 1).Value incrementedNumber = CLng(numberPart) + 1 IncrementSpecificNumberInFileName = Replace(originalName, numberPart, incrementedNumber) End Function |
応用例3:数字部分が特定の範囲になるまでインクリメント
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Function IncrementUntilLimit(ByVal originalName As String, ByVal limit As Long) As String Dim regex As Object Dim matches As Object Dim numberPart As String Dim incrementedNumber As Long '正規表現を用いて数字部分を取得 Set regex = CreateObject("VBScript.RegExp") regex.Global = True regex.Pattern = "\d+" Set matches = regex.Execute(originalName) '数字部分が存在しない場合は、そのままのファイル名を返す If matches.Count = 0 Then IncrementUntilLimit = originalName Exit Function End If numberPart = matches(0).Value incrementedNumber = CLng(numberPart) '指定された限界値になるまでインクリメント Do While incrementedNumber < limit incrementedNumber = incrementedNumber + 1 Loop IncrementUntilLimit = Replace(originalName, numberPart, incrementedNumber) End Function |
まとめ
Excel VBAを活用することで、数字を含むファイル名の数字部分を簡単にインクリ
メントすることができます。今回紹介した基本の方法から、さまざまな応用例まで、VBAを用いて効率的にファイル管理を行うための一助として活用してみてください。
VBAも良いけどパワークエリも良い
VBAの解説をしてきましたが、VBAは正直煩雑でメンテナンス性が悪いです。最近はモダンExcelと呼ばれるパワークエリやパワーピボットへのシフトが進んできています。本サイトでもパワークエリの特集をしており、サンプルデータを含む全11回の学習コンテンツでパワークエリを習得することができます。
クリックするとパワークエリの全11講座が表示されます。
-
【初心者向け】パワークエリ入門:ETLツールを使ってエクセルデータを簡単に整形・統合しよう!(1/11)
-
【実践ガイド】パワークエリでデータ収集:Excel、CSV、PDF、Webデータを簡単に取り込む方法をマスターしよう!(2/11)
-
【総力特集】パワークエリで列操作をマスター:選択、変更、移動、削除、結合、分割の詳細解説&実践テクニック!(3/11)
-
【徹底解説】パワークエリで行操作をマスター!フィルター・保持・削除テクニックと練習用エクセルで実践学習(4/11)
-
パワークエリでデータクレンジング: 文字列結合、0埋め、テキスト関数をマスター(5/11)
-
パワークエリで四捨五入、切り捨て、切り上げをマスターする方法(6/11)
-
パワークエリで効率的なデータグループ化を実現する方法(7/11)
-
パワークエリで時間と日付の計算をマスター!便利な関数を使って効率アップ(8/11)
-
パワークエリで条件別集計をマスターする方法(9/11)
-
Excelパワークエリでクロス集計表とデータベース形式を瞬時に変換する方法(10/11)
-
Excelパワークエリ入門: 効率的なデータ整理をマスターしよう!(11/11)
パワーピボットの記事はありません。興味がある場合は、書籍で学んでみてください
コメント