Realçar a célula, linha ou coluna ativa

Os seguintes exemplos de código mostram formas de realçar a célula ativa ou as linhas e colunas que incluem a célula ativa. Esses exemplos usam o evento SelectionChange do objeto Worksheet.

Código de exemplo fornecido por: Tom Urtis, Atlas programação de gerenciamento

Realçando a célula ativa

O exemplo de código a seguir limpa a cor de todas as células na planilha definindo a propriedade ColorIndex como igual a 0, e realça a célula ativa definindo a propriedade ColorIndex como igual a 8 (turquesa).

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

Realçando a linha e a coluna inteira que inclui a célula ativa

O exemplo de código a seguir limpa a cor de todas as células na planilha definindo a propriedade ColorIndex como igual a 0; em seguida, realça a linha e a coluna inteira que inclui a célula ativa definindo as propriedades EntireRow e 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

Realçando a linha e a coluna que inclui a célula ativa dentro da região atual

O exemplo de código a seguir limpa a cor de todas as células na planilha definindo a propriedade ColorIndex como igual a 0; em seguida, realça a linha e a coluna que inclui a célula ativa dentro da região atual definindo a propriedade CurrentRegion do objeto Range.

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

Sobre o colaborador

MVP Tom Urtis é o fundador da Atlas programação gerenciamento, uma empresa com soluções de gerenciamento completas de serviço do Microsoft Office e Excel localizada no Silicon Valley. Tom tem mais de 25 anos de experiência do gerenciamento de negócios e desenvolvimento dos aplicativos Microsoft e é co-autor de "Holy Macro! São 2.500 exemplos de VBA do Excel."

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.