アクティブなセル、行、または列を強調する

次のコード例では、アクティブ セル、またはアクティブ セルを含む行と列を強調表示する方法を示します。 ここでは、Worksheet オブジェクトの SelectionChange イベントが使用されます。

サンプル コードの提供元: Tom Urtis、 Atlas Programming Management

アクティブ セルを強調表示する

次のコード例では、ColorIndex プロパティを 0 に設定してワークシートのすべてのセルの色を消去し、次に ColorIndex プロパティを 8 (ターコイズ) に設定してアクティブ セルを強調表示します。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    ' Highlight the active cell
    Target.Interior.ColorIndex = 8
    Application.ScreenUpdating = True
End Sub

アクティブ セルを含む行および列全体の強調表示

次のコード例では、ColorIndex プロパティを 0 に設定して、ワークシート上のすべてのセルの色を消去し、次に EntireRow プロパティと EntireColumn プロパティを使用して、アクティブ セルを含む行および列全体を強調表示します。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 8
        .EntireColumn.Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True
End Sub

アクティブ セル領域内でのアクティブ セルを含む行および列の強調表示

次のコード例では、ColorIndex プロパティを 0 に設定して、ワークシート上のすべてのセルの色を消去し、次に Range オブジェクトの CurrentRegion プロパティを使用して、アクティブ セル領域内で、アクティブ セルを含む行および列を強調表示します。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    If IsEmpty(Target) Or Target.Cells.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    With ActiveCell
        ' Highlight the row and column that contain the active cell, within the current region
        Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 8
        Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True
End Sub

投稿者について

MVP Tom Urtis は、シリコン バレーにある Atlas Programming Management の創業者です。Atlas Programming Management は、Microsoft Office および Excel に関するあらゆるビジネス ソリューションを提供する企業です。 Tom は 25 年以上のビジネス管理および Microsoft Office アプリケーション開発の経験があり、『Holy Macro! It's 2,500 Excel VBA Examples』の 共著者でもあります。

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。