Como: Alterar a formatação em linhas da planilha contendo selecionado de células

Você pode alterar a fonte de uma linha inteira que contém uma célula selecionada para que o texto está em negrito.

Aplicável a: As informações neste tópico se aplicam a projetos de nível de documento e projetos de nível de aplicativo para Excel 2007 e Excel 2010. Para obter mais informações, consulte Recursos disponíveis pelo aplicativo do Office e o tipo de projeto.

Para aplicar negrito a linha atual e o anteriormente em negrito linha normal

  1. Declare uma variável estática para controlar a linha selecionada anteriormente.

    Static previousRow As Integer = 0
    
    static int previousRow = 0;
    
  2. Recuperar uma referência à célula atual usando o ActiveCell propriedade.

    Dim currentCell As Excel.Range = Me.Application.ActiveCell
    
    Excel.Range currentCell = this.Application.ActiveCell;
    
  3. Estilo da atual linha negrito usando o EntireRow a propriedade da célula ativa.

    currentCell.EntireRow.Font.Bold = True
    
    currentCell.EntireRow.Font.Bold = true; 
    
  4. Certifique-se de que o valor atual de previousRow é não 0. Um 0 (zero) indica que esta é a primeira vez que esse código.

    If previousRow <> 0 Then
    
    if (previousRow != 0)
    
  5. Certifique-se de que a linha atual é diferente da linha anterior.

    If currentCell.Row <> previousRow Then
    
    if (currentCell.Row != previousRow)
    
  6. Recuperar uma referência a um intervalo que representa a linha que foi anteriormente marcada e um conjunto que não seja de linha em negrito.

    Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
    rng.EntireRow.Font.Bold = False
    
    Excel.Range rng = (Excel.Range)ws.Rows[previousRow, missing];
    rng.EntireRow.Font.Bold = false;
    
  7. Armazene a linha atual, que pode se tornar o próximo passo da linha anterior.

    previousRow = currentCell.Row
    
    previousRow = currentCell.Row;
    

O exemplo a seguir mostra o método complete.

Exemplo

Private Sub BoldCurrentRow(ByVal ws As Excel.Worksheet)

    ' Keep track of the previously bolded row.
    Static previousRow As Integer = 0

    ' Work with the current active cell.
    Dim currentCell As Excel.Range = Me.Application.ActiveCell

    ' Bold the current row.
    currentCell.EntireRow.Font.Bold = True

    ' If a pass has been done previously, make the old row not bold.
    ' Make sure previousRow is not 0 (otherwise this is your first pass through).
    If previousRow <> 0 Then

        ' Make sure the current row is not the same as the previous row.
        If currentCell.Row <> previousRow Then

            Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
            rng.EntireRow.Font.Bold = False
        End If
    End If

    ' Store the new row number for the next pass.
    previousRow = currentCell.Row
End Sub
// Keep track of the previously bolded row.
static int previousRow = 0;

private void BoldCurrentRow(Excel.Worksheet ws)
{
    // Work with the current active cell.
    Excel.Range currentCell = this.Application.ActiveCell;

    // Bold the current row.
    currentCell.EntireRow.Font.Bold = true; 

    // If a pass has been done previously, make the old row not bold.
    // Make sure previousRow is not 0 (otherwise this is your first pass through).
    if (previousRow != 0)

        // Make sure the current row is not the same as the previous row.
        if (currentCell.Row != previousRow)
        {
            Excel.Range rng = (Excel.Range)ws.Rows[previousRow, missing];
            rng.EntireRow.Font.Bold = false;
        }

    // Store the new row number for the next pass.
    previousRow = currentCell.Row;
}

Consulte também

Tarefas

Como: Aplicar estilos a intervalos em pastas de trabalho

Como: Copiar dados e formatação em planilhas

Conceitos

Trabalhando com planilhas

Parâmetros opcionais em soluções do Office