📘 Excel逆引き事典

【VBA】全てのシートを完全一致で背景色を変える方法

手作業で大量のデータの背景色を変更するのは大変です。この記事では、特定の文字列と完全一致するセルの背景色を一括で変更する方法を紹介します。

サンプルコード

VBA
Option Explicit
Sub ChangeBackgroundColor()
    Dim ws As Worksheet
    Dim searchStr As String
    Dim cell As Range
    
    ' 検索文字列を指定
    searchStr = "特定の文字列"
    
    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        Set cell = ws.Cells.Find(What:=searchStr, LookIn:=xlValues, LookAt:=xlWhole)
        If Not cell Is Nothing Then
            cell.Interior.ColorIndex = 6 ' 背景色を変更(ここでは青色)
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

よくある質問

Q 元に戻せますか?

A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。

Q エラーが出たら?

A.
シート名や列番号が正しいか確認してください。