【VBA】正規表現を使って特定の文字を色付けする方法
はじめに
Excel VBA を使用して、テキストファイルの内容を検索し、正規表現に一致する部分を色付けするスクリプト を作成する。
具体的には、
- 指定フォルダ内のテキストファイルを検索
- 正規表現に一致する行をシートに記載
- 一致した文字列に赤色の強調を適用
また、アクティブセル内の文字列に対して 正規表現で一致する部分のみを赤くする補助スクリプト も用意する。
スクリプト
1. テキストファイルを検索して色付けする VBA
Sub ProcessFiles()
Dim lastRow As Long
Dim i As Long
Dim fileName As String
Dim filePath As String
Dim fileContent As String
Dim regex As Object
Dim matches As Object
Dim pattern1 As String
Dim pattern2 As String
Dim fileNum As Integer
Dim line As String
Dim fullPath As String
' アクティブシートを取得
Dim ws As Worksheet
Set ws = ActiveSheet
' 最後の行を取得
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' 正規表現のパターン
pattern1 = "正規表現1" ' 1つ目の正規表現をここに記載
pattern2 = "正規表現2" ' 2つ目の正規表現をここに記載
' 正規表現オブジェクトの作成
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
' 1行ずつ処理
For i = 1 To lastRow
fileName = ws.Cells(i, "A").Value ' A列のファイル名を取得
filePath = "C:\path\to\folder\" & fileName & ".txt" ' 適切なフォルダパスに変更
' ファイルが存在するか確認
If Dir(filePath) <> "" Then
' ファイルを開いて内容を1行ずつ読み取る
fileNum = FreeFile
Open filePath For Input As fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, line
' 1つ目の正規表現に一致するか確認
regex.Pattern = pattern1
If regex.Test(line) Then
' 一致した場合、その行を AB 列に記載
ws.Cells(i, "AB").Value = line
' 2つ目の正規表現に一致する部分を赤く塗る
regex.Pattern = pattern2
Set matches = regex.Execute(line)
If matches.Count > 0 Then
Dim match As Object
For Each match In matches
' マッチ部分を赤くする
Dim startIndex As Integer
startIndex = InStr(line, match.Value)
If startIndex > 0 Then
ws.Cells(i, "AB").Characters(startIndex, match.Length).Font.Color = RGB(255, 0, 0)
End If
Next match
End If
' 1つ目のパターンに一致したら次のファイルへ
Exit Do
End If
Loop
' ファイルを閉じる
Close fileNum
Else
' ファイルがない場合の処理
ws.Cells(i, "AB").Value = "ファイルが見つかりません"
End If
Next i
' 終了メッセージ
MsgBox "処理が完了しました。"
End Sub
2. アクティブセル内の文字列を色付けする VBA
Sub HighlightMatchesInActiveCell()
Dim regex As Object
Dim matches As Object
Dim pattern2 As String
Dim cellContent As String
Dim match As Object
Dim matchStart As Integer
' アクティブセルの内容を取得
cellContent = ActiveCell.Value
' 2つ目の正規表現のパターンを設定
pattern2 = "正規表現2" ' 2つ目の正規表現をここに記載
' 正規表現オブジェクトの作成
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
' 2つ目の正規表現に一致する部分を探す
regex.Pattern = pattern2
Set matches = regex.Execute(cellContent)
' 一致した部分があれば色をつける
If matches.Count > 0 Then
For Each match In matches
' 一致した部分の開始位置を取得
matchStart = InStr(cellContent, match.Value)
' 一致した部分を赤くする
If matchStart > 0 Then
ActiveCell.Characters(matchStart, Len(match.Value)).Font.Color = RGB(255, 0, 0)
End If
Next match
Else
MsgBox "正規表現に一致する部分がありません。"
End If
End Sub
スクリプトの解説
1. ProcessFiles()
- A列のファイル名を取得し、対応するテキストファイルを開く
- 1つ目の正規表現に一致する行を取得し、AB列に記載
- 2つ目の正規表現に一致する部分のみを赤くする
ws.Cells(i, "AB").Characters(startIndex, match.Length).Font.Color = RGB(255, 0, 0)
この処理により、特定の単語だけを強調表示 する。
2. HighlightMatchesInActiveCell()
- アクティブセル内の文字列を対象に検索
- 一致する部分だけを赤色にする
- 一致しなかった場合はメッセージを表示
If matchStart > 0 Then
ActiveCell.Characters(matchStart, Len(match.Value)).Font.Color = RGB(255, 0, 0)
End If
これにより、ユーザーが手動で入力したデータに対しても強調表示が可能。
動作の流れ
① ProcessFiles()
を実行
- シートA列のファイル名をもとに、対応するテキストファイルを検索
- 該当するテキストファイルの内容を1行ずつ読み取る
- 1つ目の正規表現に一致した場合、その行をAB列に記載
- 2つ目の正規表現に一致する部分を赤色に変更
② HighlightMatchesInActiveCell()
を実行
- アクティブセルのテキストを取得
- 正規表現に一致する部分のみ赤色で強調
まとめ
このVBAスクリプトを活用することで、
✅ テキストファイルを検索し、指定条件に一致する行をExcelに自動記載
✅ 該当する単語だけを赤色で強調表示
✅ アクティブセルのテキストも強調可能
特定のパターンをすぐに視覚的に把握したい場合に便利なので、用途に合わせてカスタマイズしてみてほしい。